本文介绍了默认情况下禁用ASP.NET MVC用户帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在MVC 5中使用默认的ASP.NET身份用户管理器.是否有任何方法可以允许用户注册,但默认情况下将其禁用?我想让管理员手动批准并启用帐户.
I'm using the default ASP.NET identity user manager in MVC 5. Is there any way to allow users to register, but have them disabled by default? I'd like to have an admin manually approve and enable accounts.
推荐答案
向应用程序用户添加新属性
Add new property to Application user
public partial class ApplicationUser : IdentityUser
{
public Boolean IsApproved { get; set; }
}
然后您可以在ApplicationUserManager
类中添加实现CheckPasswordAsync
,覆盖其在UserManager
中的晚饭方法,如下所示
Then you can add implement CheckPasswordAsync
in ApplicationUserManager
class overriding its supper method in UserManager
like below
public override Task<bool> CheckPasswordAsync(ApplicationUser user, string password)
{
if (!user.IsApproved )
{
return Task.FromResult<bool>(false);
}
return base.CheckPasswordAsync(user, password);
}
此方法的唯一缺点是,即使对于未经批准的用户,我们也会收到无效的登录尝试"消息.
Only disadvantage of this approach is we get "Invalid login attempt" message even for unapproved users.
这篇关于默认情况下禁用ASP.NET MVC用户帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!