2

我已经使用 SiteCore 7 大约一个月或更短的时间了。

我无法从其中的外部数据库设置 .Net Membership 提供程序。

这是我的课的样子:

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>}

我的问题是:

  1. 有没有人在 SiteCore 中与 .Net Membership Provider 合作过?一个。如果是这样,我做错了什么?
  2. 有人知道 SiteCore 中 .Net 成员资格的工作示例吗?

非常感激 !

戴夫

4

1 回答 1

3

几天前,我实现了会员提供者和角色提供者,以允许具有特定角色的外部数据库的用户登录到 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;
}

}

于 2013-08-14T21:16:25.183 回答