SiteCore.Net会员示例
本文关键字:Net SiteCore | 更新日期: 2023-09-27 18:27:46
我已经使用SiteCore 7大约一个月或更短时间了。
我无法从外部数据库中设置.Net成员资格提供程序。
这就是我的课的样子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Security;
namespace MProvider
{
public abstract class MemberProvider : MembershipProvider
{
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
base.Initialize(name, config);
}
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
throw new NotImplementedException();
}
public override bool ValidateUser(string username, string password)
{
throw new NotImplementedException();
}
}
}
}
这是SiteCore 中的web.config
<switchingProviders>
<membership>
<provider providerName="sql" storeFullNames="true" wildcard="%" domains="*"/>
<provider providerName="MProvider" storeFullNames="true" wildcard="%" domains="*"/>
<membership defaultProvider="sitecore" hashAlgorithmType="SHA1">
<providers>
<clear/>
<add name="sitecore" type="Sitecore.Security.SitecoreMembershipProvider, Sitecore.Kernel" realProviderName="MProvider" providerWildcard="%" raiseEvents="true"/>
<add name="sql" type="System.Web.Security.SqlMembershipProvider" connectionStringName="core" applicationName="sitecore" minRequiredPasswordLength="1" minRequiredNonalphanumericCharacters="0" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="256"/>
<add name="MProvider" type="Sitecore.Security.SitecoreMembershipProvider" realProviderName="MProvider"
applicationName="sitecore" minRequiredPasswordLength="1"
minRequiredNonalphanumericCharacters="0"></add>
</providers>
</membership>}
我的问题是:
- 有人在SiteCore中与.Net会员提供商合作过吗?a.如果是,我做错了什么
- 有人知道SiteCore中.Net成员身份的工作示例吗
非常感谢!
Dave
几天前,我实现了成员资格提供者和角色提供者,允许外部数据库中具有特定角色的用户登录到sitecore。这个答案对我帮助很大。https://stackoverflow.com/a/6954337/421490
这是我的会员资格提供者实现。我打扫了这节课。所有其他方法和属性都被覆盖并引发NotImplementedException。
public class FrontEndMembershipProvider : MembershipProvider
{
public override bool ValidateUser(string username, string password)
{
var webSecurity = DependencyResolver.Current.GetService<IWebSecurity>();
var cleanUsername = CleanUsername(this.Name, username);
if (!string.IsNullOrEmpty(cleanUsername))
{
return webSecurity.ValidateUser(cleanUsername, password) != null;
}
return false;
}
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
var usersRepository = DependencyResolver.Current.GetService<IUsersRepository>();
var user = usersRepository.GetById((int)providerUserKey);
if (user == null)
{
return null;
}
return this.CreateMembershipUser(user);
}
public override MembershipUser GetUser(string username, bool userIsOnline)
{
var usersRepository = DependencyResolver.Current.GetService<IUsersRepository>();
var cleanUsername = CleanUsername(this.Name, username);
if (!string.IsNullOrEmpty(cleanUsername))
{
var user = usersRepository.GetByEmail(cleanUsername);
if (user != null)
{
return this.CreateMembershipUser(user);
}
}
return null;
}
public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
{
var usersRepository = DependencyResolver.Current.GetService<IUsersRepository>();
var membershipUserCollection = new MembershipUserCollection();
var allUsersWithRole = usersRepository.GetAllUsersForSitecore(pageIndex + 1, pageSize);
foreach (var user in allUsersWithRole)
{
membershipUserCollection.Add(this.CreateMembershipUser(user));
}
totalRecords = allUsersWithRole.TotalItemCount;
return membershipUserCollection;
}
private MembershipUser CreateMembershipUser(User user)
{
return new MembershipUser(this.Name, UpgradeUsername(this.Name, user.Email), user.Id, user.Email, null, null, user.UserAccountStateId == (int)UserAccountStateType.Active, user.UserAccountStateId == (int)UserAccountStateType.Banned, user.CreationDate, user.LastActivityDate ?? user.CreationDate, user.LastActivityDate ?? user.CreationDate, user.CreationDate, user.CreationDate);
}
public static string CleanUsername(string domain, string username)
{
if (!string.IsNullOrEmpty(username))
{
if (username.Contains("''"))
{
var parts = username.Split('''');
return parts[0] == domain ? parts[1] : null;
}
}
return username;
}
public static string UpgradeUsername(string domain, string username)
{
if (!string.IsNullOrEmpty(username))
{
if (username.Contains("''"))
{
var parts = username.Split('''');
if (parts[0] == domain)
{
return username;
}
}
return domain + "''" + username;
}
return username;
}
}