当您创建安装了身份位的站点时,您的站点将有一个名为“IdentityModels.cs”的文件。在这个文件中有一个名为ApplicationUser的类,它继承自IdentityUser。
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://devblogs.microsoft.com/aspnet/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates/ to learn more.
public class ApplicationUser : IdentityUser
评论里有一个很好的链接,方便点击here https://devblogs.microsoft.com/aspnet/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates/
本教程准确地告诉您为用户添加自定义属性需要执行哪些操作。
实际上,甚至不用费心去看教程。
-
向 ApplicationUser 类添加一个属性,例如:
公共布尔? IsEnabled { 获取;放; }
-
在数据库的 AspNetUsers 表中添加一个具有相同名称的列。
-
繁荣,就是这样!
现在在您的 AccountController 中,您有一个注册操作,如下所示:
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, IsEnabled = true };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
我在创建 ApplicationUser 对象时添加了 IsEnabled = true 。该值现在将保留在 AspNetUsers 表的新列中。
然后,您需要在登录过程中通过重写 ApplicationSignInManager 中的 PasswordSignInAsync 来检查此值。
我是这样做的:
public override Task<SignInStatus> PasswordSignInAsync(string userName, string password, bool rememberMe, bool shouldLockout)
{
var user = UserManager.FindByEmailAsync(userName).Result;
if ((user.IsEnabled.HasValue && !user.IsEnabled.Value) || !user.IsEnabled.HasValue)
{
return Task.FromResult<SignInStatus>(SignInStatus.LockedOut);
}
return base.PasswordSignInAsync(userName, password, rememberMe, shouldLockout);
}
您的里程可能会有所不同,并且您可能不想返回 SignInStatus,但您明白了。