我正在开发一个新的 ASP.NET MVC 项目,使用存储在数据库中的个人帐户进行身份验证。这是我的类,每次测试时都会使用示例数据为数据库播种:
public class DevelopmentInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
{
protected override void Seed(ApplicationDbContext context)
{
base.Seed(context);
var applicationUserManager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
var sampleUserOne = new ApplicationUser { UserName = "SampleUser", Email = "[email protected] /cdn-cgi/l/email-protection" };
var result = applicationUserManager.Create(sampleUserOne, "aaaaaa");
if (!result.Succeeded)
throw new Exception();
context.SaveChanges();
}
}
The Login
操作与模板中一样:
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.Email, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
问题描述很简单:尝试使用种子用户的凭据登录失败。
具体来说,FindAsync
方法返回null
,即使用户存在于数据库中 -FindByEmailAsync
确实找到了种子用户。
但是,创建一个新帐户可以工作并允许我登录。
为什么我无法以种子用户身份登录,即使我可以注册新帐户并使用该帐户登录?
我怀疑这与密码的散列方式有关,但我不知道如何确认这一点。
难道是我账号下错了?我不应该创建一个单独的ApplicationUserManager
in the Seed
方法?如果没有,我应该如何获得一个以便打电话Create
?在我的帐户被锁定或用户在部署的应用程序中被锁定之前,我试图了解新系统是如何工作的。