4

我们需要在 ASP.NET WebFroms 应用程序中同时支持两个成员资格提供程序。Indeed 用户通过勾选复选框表明他/她想通过Asp Net Sql Membership ProviderAsp Net Active Directory Membership Provider

我的想法是根据 Checkbox 选中值以编程方式更改成员资格提供程序。但不知道如何才能做到这一点。

非常感谢任何帮助。

4

2 回答 2

6

您需要的是一个组合的提供者,它包装了两个真实的提供者和一个可以确定应该使用哪个提供者的服务。

更新

CompositeProvider可能看起来像这样,并且可以像任何其他提供商一样在您的 web.config 中注册。

public class CompositeProvider : MembershipProvider
{
    private MembershipProvider SqlProvider
    {
        get { return Membership.Providers["Sql Provider"]; }
    }

    private MembershipProvider AdProvider
    {
        get { return Membership.Providers["Ad Provider"]; }
    }

    // This property implements the logic that knows how
    // to switch between the two providers.
    private MembershipProvider Provider
    {
        get
        {
            var context = HttpContext.Current;

            if (context != null)
            {
                var session = context.Session;

                if (session != null &&
                    session["use_ad_provider"] == "true")
                {
                    return AdProvider;
                }
            }

            return SqlProvider;
        }
    }

    public override bool ChangePassword(string username, 
        string oldPassword, string newPassword)
    {
        return Provider.ChangePassword(username, oldPassword, newPassword);
    }

    public override bool DeleteUser(string username, 
        bool deleteAllRelatedData)
    {
        return Provider.DeleteUser(username, deleteAllRelatedData);
    }

    public override bool EnablePasswordReset
    {
        get { return Provider.EnablePasswordReset; }
    }

    public override bool EnablePasswordRetrieval
    {
        get { return Provider.EnablePasswordRetrieval; }
    }

    // TODO: Implementation of all other members.
}
于 2013-04-19T13:02:24.573 回答
2

这基本上与Steven's 的答案相同,但有一个完全实现的类(以节省其他人写这一切的时间)。

public class CompositeProvider : MembershipProvider
{

    public override string ApplicationName
    {
        get
        {
            return _provider.ApplicationName;
        }
        set
        {
            _provider.ApplicationName = value;
        }
    }

    private MembershipProvider _provider
    {
        get
        {
            // Insert your logic here to change between multiple providers
            if(SomeCondition == true)
                return Membership.Providers["AspNetSqlMembershipProvider"];
            else
                return Membership.Providers["AspNetSqlMembershipProvider2"];
        }
    }

    public override bool ChangePassword(string username, string oldPassword, string newPassword)
    {
        return _provider.ChangePassword(username, oldPassword, newPassword);
    }

    public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
    {
        return _provider.ChangePasswordQuestionAndAnswer(username, password, newPasswordQuestion, newPasswordAnswer);
    }

    public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
    {
        return _provider.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
    }

    public override bool DeleteUser(string username, bool deleteAllRelatedData)
    {
        return _provider.DeleteUser(username, deleteAllRelatedData);
    }

    public override bool EnablePasswordReset
    {
        get { return _provider.EnablePasswordReset; }
    }

    public override bool EnablePasswordRetrieval
    {
        get { return _provider.EnablePasswordRetrieval; }
    }

    public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
    {
        return _provider.FindUsersByEmail(emailToMatch, pageIndex, pageSize, out totalRecords);
    }

    public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
    {
        return _provider.FindUsersByName(usernameToMatch, pageIndex, pageSize, out totalRecords);
    }

    public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
    {
        return _provider.GetAllUsers(pageIndex, pageSize, out totalRecords);
    }

    public override int GetNumberOfUsersOnline()
    {
        return _provider.GetNumberOfUsersOnline();
    }

    public override string GetPassword(string username, string answer)
    {
        return _provider.GetPassword(username, answer);
    }

    public override MembershipUser GetUser(string username, bool userIsOnline)
    {
        return _provider.GetUser(username, userIsOnline);
    }

    public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
    {
        return _provider.GetUser(providerUserKey, userIsOnline);
    }

    public override string GetUserNameByEmail(string email)
    {
        return _provider.GetUserNameByEmail(email);
    }

    public override int MaxInvalidPasswordAttempts
    {
        get { return _provider.MaxInvalidPasswordAttempts; }
    }

    public override int MinRequiredNonAlphanumericCharacters
    {
        get { return _provider.MinRequiredNonAlphanumericCharacters; }
    }

    public override int MinRequiredPasswordLength
    {
        get { return _provider.MinRequiredPasswordLength; }
    }

    public override int PasswordAttemptWindow
    {
        get { return _provider.PasswordAttemptWindow; }
    }

    public override MembershipPasswordFormat PasswordFormat
    {
        get { return _provider.PasswordFormat; }
    }

    public override string PasswordStrengthRegularExpression
    {
        get { return _provider.PasswordStrengthRegularExpression; }
    }

    public override bool RequiresQuestionAndAnswer
    {
        get { return _provider.RequiresQuestionAndAnswer; }
    }

    public override bool RequiresUniqueEmail
    {
        get { return _provider.RequiresUniqueEmail; }
    }

    public override string ResetPassword(string username, string answer)
    {
        return _provider.ResetPassword(username, answer);
    }

    public override bool UnlockUser(string userName)
    {
        return _provider.UnlockUser(userName);
    }

    public override void UpdateUser(MembershipUser user)
    {
        _provider.UpdateUser(user);
    }

    public override bool ValidateUser(string username, string password)
    {
        return _provider.ValidateUser(username, password);
    }
}
于 2013-09-13T14:33:17.060 回答