我们需要在ASP.NET WebFroms应用程序中同时支持两个成员资格提供程序。用户通过勾选复选框表示希望通过Asp Net Sql Membership Provider
或Asp Net Active Directory Membership Provider
进行登录。
我的想法是根据复选框的选中值以编程方式更改成员资格提供程序。但不知道如何实现。
非常感谢任何帮助。
我们需要在ASP.NET WebFroms应用程序中同时支持两个成员资格提供程序。用户通过勾选复选框表示希望通过Asp Net Sql Membership Provider
或Asp Net Active Directory Membership Provider
进行登录。
我的想法是根据复选框的选中值以编程方式更改成员资格提供程序。但不知道如何实现。
非常感谢任何帮助。
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.
}
这基本上是与Steven相同的答案,但具有完全实现的类(为了节省其他人编写全部内容的时间)。
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);
}
}