按代码更改ASP.NET成员资格提供程序

时间:2013-04-19 12:57:48

标签: asp.net asp.net-membership

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

登录

我的想法是根据Checkbox选中的值以编程方式更改成员资格提供程序。但不知道怎么能这样做。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分: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.
}

答案 1 :(得分: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);
    }
}