我在代码中创建了用户问题。它们在前几次登录尝试时工作,但稍后停止登录并从findasync方法返回null。如果我做了一个findbyname,它会返回用户,但是这并没有检查密码,所以我无法使用它。这些用户也没有密码哈希,但我使用注册方法创建的用户可以。
if (!db.Users.Any())
{
var userStore = new UserStore<ApplicationUser>(db);
var userManager = new ApplicationUserManager(userStore);
var user = new ApplicationUser
{
UserName = "username",
FirstName = "first",
LastName = "last",
EmailAddress = "example@example.com",
DateCreated = DateTime.Now,
DateModified = DateTime.Now,
ContactNumber = "0208555555",
LastActivityDate = DateTime.Now,
LastPasswordChangedDate = DateTime.Now
};
user.LockoutEnabled = false;
userManager.Create(user, "abc123");
userManager.AddToRole(user.Id, "Admin");
}
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.UserName, model.Password);
var user2 = await UserManager.FindByNameAsync(model.UserName);
PasswordVerificationResult result = PasswordVerificationResult.Failed;
result = UserManager.PasswordHasher.VerifyHashedPassword(user2.PasswordHash, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
AuditLog.LogUserAction(UserManager.FindByName(model.UserName).Id, "Login", "User successfully logged in.");
user.LastActivityDate = DateTime.Now;
await db.SaveChangesAsync();
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
public class ApplicationUser : IdentityUser
{
[Required]
[Display(Name = "First name")]
[MaxLength(20)]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last name")]
[MaxLength(50)]
public string LastName { get; set; }
[Required]
[Display(Name = "Email")]
[EmailAddress(ErrorMessage = "Invalid email address")]
public string EmailAddress { get; set; }
[Required]
[Display(Name = "Contact number")]
public string ContactNumber { get; set; }
[Required]
[Display(Name = "Service Provider")]
public int SK_ServiceProviderID { get; set; }
public virtual ServiceProvider ServiceProvider { get; set; }
[Required]
[DisplayName("Date Created")]
public DateTime DateCreated { get; set; }
[Required]
[DisplayName("Date Modified")]
public DateTime DateModified { get; set; }
[Required]
public DateTime LastPasswordChangedDate { get; set; }
[Required]
public DateTime LastActivityDate { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
答案 0 :(得分:0)
结果证明它失败了,因为在按代码创建用户时我没有设置安全标记字段,使用以下内容时不能为空:
var resetToken = await UserManager.GeneratePasswordResetTokenAsync(model.UserId);
IdentityResult passwordChangeResult = await UserManager.ResetPasswordAsync(model.UserId, resetToken, model.Password);
This回答另一个问题帮我搞清楚了。