为什么我不能以种子用户身份登录?

The*_*kis 4 asp.net-mvc-5 asp.net-identity

我正在开发一个新的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 = "sample@example.com" };
        var result = applicationUserManager.Create(sampleUserOne, "aaaaaa");

        if (!result.Succeeded) 
            throw new Exception();

        context.SaveChanges();
    }
}
Run Code Online (Sandbox Code Playgroud)

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);
    }
Run Code Online (Sandbox Code Playgroud)

问题的描述非常简单:尝试使用种子用户的凭据登录失败.

具体来说,即使用户出现在数据库中,该FindAsync方法null也会返回- FindByEmailAsync确实找到了种子用户.

但是,创建新帐户有效并允许我登录.

为什么我不能以播种用户身份登录,即使我可以注册一个新帐户并使用该帐户登录?

我怀疑它与密码的散列方式有关,但我不知道如何确认.

我播种帐号错了吗?我不应该ApplicationUserManagerSeed方法中创建单独的吗?如果没有,我该如何才能打电话Create?我试图了解新系统的工作原理,最后锁定我的帐户或用户最终被锁定在已部署的应用程序中.

Bre*_*een 6

以下代码:

var user = await UserManager.FindAsync(model.Email, model.Password);
Run Code Online (Sandbox Code Playgroud)

期望传递userName,而不是电子邮件地址.

这个简单的改变应该照顾好事情:

var user = await UserManager.FindAsync(model.UserName, model.Password);
Run Code Online (Sandbox Code Playgroud)