如何在我的自定义表而不是aspnet用户中使用哈希密码保存新记录?

我使用asp.net身份创建新用户但收到错误:

无法将值NULL插入列’Id’,表’Mydb.dbo.AspNetUsers’; 列不允许空值。 INSERT失败。\ r \ n语句已终止

但是在这里我没有像AspNetUsers这样的表,而是我拥有自己的用户表。

代码: Web.config:2个连接字符串

  

IdentityModel.cs:

 public class ApplicationUser : IdentityUser { public async Task GenerateUserIdentityAsync(UserManager manager) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType this.SecurityStamp = Guid.NewGuid().ToString(); var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); // Add custom user claims here return userIdentity; } public string Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public virtual string Email { get; set; } public string Password { get; set; } public string Role { get; set; } public Nullable IsActive { get; set; } public Nullable CreatedBy { get; set; } public Nullable CreatedDate { get; set; } public Nullable LastLogin { get; set; } public ApplicationUser() { } public ApplicationUser(string email, string firstName, string lastName, string designation, bool isActive) { Email = email; FirstName = firstName; LastName = lastName; Designation = designation; IsActive = isActive; } } public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext() : base("MyConnString", throwIfV1Schema: false) { } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } } 

UserStore1.cs:

 public class UserStore1 : IUserStore, IUserPasswordStore { private readonly HttpContext _httpContext; UserStore userStore = new UserStore(new ApplicationDbContext()); public System.Threading.Tasks.Task CreateAsync(ApplicationUser user) { HttpContext.Current = _httpContext ?? HttpContext.Current; var context = userStore.Context as ApplicationDbContext; context.Users.Add(user); context.Configuration.ValidateOnSaveEnabled = false; context.SaveChanges(); return Task.FromResult(true); } } 

控制器:

  [Authorize] public class AccountController : Controller { public AccountController() : this(new UserManager(new UserStore1())) { } public AccountController(UserManager userManager) { UserManager = userManager; } public UserManager UserManager { get; private set; } [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task Login(string email, string password, bool rememberMe = false, string returnUrl = null) { if (ModelState.IsValid) { var user = new ApplicationUser { FirstName= "Abc", LastName= "Pqr", UserName="Abc@yahoo.com", SecurityStamp = Guid.NewGuid().ToString() }; var result= await UserManager.CreateAsync(user,"123456"); } return View(); } } 

注意:我在数据库表字段中有自动生成的Id,而Id是Int。

更新:我首先使用数据库(edmx),我使用的表是用于插入新记录的自定义表(例如: 用户 )。

起初我已经实现了microsoft asp.net身份,如下面的问题所示,但是1位用户指出我没有使用负责处理登录,cookies等的ApplicationUser类 ,所以我现在尝试使用ApplicationUser类:

如何自定义实现asp.net身份的UpdateAsync方法?

我现在真的很遗憾我决定选择Microsoft Identity Framework进行身份validation,因为我真的觉得它很复杂,但现在我已经向前迈进了,我必须顺其自然。

我发现,在ApplicationUser类中,您声明属性IdEmail是错误的,因为IdentityUser类已经具有这些属性。 这可能会引发这个问题。 但是如果需要,你可以覆盖它们。 您使用的构造函数也不是必需的。 ApplicationUser类应该是:

 public class ApplicationUser : IdentityUser { public async Task GenerateUserIdentityAsync(UserManager manager) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType this.SecurityStamp = Guid.NewGuid().ToString(); var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); // Add custom user claims here return userIdentity; } public string FirstName { get; set; } public string LastName { get; set; } public string Password { get; set; } public string Role { get; set; } public bool? IsActive { get; set; } public int? CreatedBy { get; set; } public DateTime? CreatedDate { get; set; } public DateTime? LastLogin { get; set; } } 

第二件事,您在Login操作中创建用户也是无效的。 你应该在Register动作中做到这一点。 以下是一个例子:

  public async Task Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { FirstName = "Abc", LastName = "Pqr", UserName = "Abc@yahoo.com", Email= model.Email, Password= model.Password, PasswordHash = UserManager.PasswordHasher.HashPassword(model.Password), SecurityStamp = Guid.NewGuid().ToString() }; var result = await UserManager.CreateAsync(user); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); return RedirectToAction("Index", "Home"); } AddErrors(result); } return View(model); } 

希望这会有所帮助:)