在我的自定义会员提供程序中,我已经为 ValidateUser 提供了实现,到目前为止一切都很好,但现在突然 PasswordFormat 开始抛出
NotImplementedException 未被用户代码处理
我的 web.config 或自定义会员资格没有变化。一些堆栈溢出答案提到在 Web.config 中的 system.web 下添加角色管理器,但它对我不起作用。另外,我已经尝试了乔什here建议的答案,但仍然没有区别。我正在使用十六进制编码。
除了 ValidateUser 之外,其他都没有在我的自定义成员资格中实现,如下
public override bool ValidateUser(string username, string password)
{
LoginModel model = new LoginModel
{
UserName = username,
Password = password,
ErrorMessage = ""
};
User user = UserManager.AuthenticateUser(model);
if (user != null)
{
HttpContext.Current.Items.Add("User", user);
return true;
}
return false;
}
在这里分享我的解决方案,以防其他人需要它。
PasswordFormat 值可以在 Web 配置中定义,然后通过读取 MembershipPasswordFormat 的 PasswordFormat 中的值来提供实现,如here所述。
public override MembershipPasswordFormat PasswordFormat
{
string temp_format = config["passwordFormat"];
if (temp_format == null)
{
temp_format = "Hashed";
}
switch (temp_format)
{
case "Hashed":
passwordFormat = MembershipPasswordFormat.Hashed;
break;
case "Encrypted":
passwordFormat = MembershipPasswordFormat.Encrypted;
break;
case "Clear":
passwordFormat = MembershipPasswordFormat.Clear;
break;
default:
throw new ProviderException("Password format not supported.");
}
}
如果有人不想在 web.config 中设置该值,也可以在会员密码格式中设置,因为会员资格内容在应用程序的生命周期中几乎不会改变。