当您创建安装了身份位的站点时,您的站点将有一个名为“IdentityModels.cs”的文件。在这个文件中有一个名为 ApplicationUser 的类,它继承自 IdentityUser。
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
那里的cmets中有一个不错的链接,为方便点击here
本教程准确地告诉您为您的用户添加自定义属性需要做什么。
实际上,甚至不必费心看教程。
1) 给ApplicationUser类添加一个属性,例如:
public bool? IsEnabled { get; set; }
2) 在数据库的 AspNetUsers 表中添加同名列。
3) 繁荣,就是这样!
现在在您的 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,但您明白了。