4

ASP.NET WebFroms アプリケーションで 2 つのメンバーシップ プロバイダーを同時にサポートする必要があります。Indeed ユーザーは、チェックボックスをオンにすることで、Asp Net Sql Membership Providerまたは経由でログインしたいことを示します。Asp Net Active Directory Membership Provider

私の考えは、チェックボックスのチェック値に基づいてプログラムでメンバーシッププロバイダーを変更することです。しかし、これを行う方法がわからない。

どんな助けでも大歓迎です。

4

2 に答える 2

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 に答える