为什么我不能以种子用户身份登录? [英] Why can't I log in as the seeded user?
问题描述
我正在使用一个存储在数据库中的个人帐户进行身份验证,正在开发一个新的ASP.NET MVC项目.这是我的课程,每次测试时,它将使用示例数据为数据库播种:
公共类DevelopmentInitializer:DropCreateDatabaseAlways< ApplicationDbContext>{受保护的重写void Seed(ApplicationDbContext上下文){base.Seed(context);var applicationUserManager = new ApplicationUserManager(new UserStore< ApplicationUser>(上下文));var sampleUserOne = new ApplicationUser {UserName ="SampleUser",电子邮件="sample@example.com"};var result = applicationUserManager.Create(sampleUserOne,"aaaaaa");如果(!结果成功)抛出新的Exception();context.SaveChanges();}}
Login
操作与模板中的操作相同:
////POST:/帐户/登录[HttpPost][AllowAnonymous][ValidateAntiForgeryToken]公共异步Task< ActionResult>Login(LoginViewModel模型,字符串returnUrl){如果(ModelState.IsValid){var user =等待UserManager.FindAsync(model.Email,model.Password);如果(用户!= null){等待SignInAsync(user,model.RememberMe);返回RedirectToLocal(returnUrl);}别的{ModelState.AddModelError(",无效的用户名或密码.");}}//如果到此为止,则出现故障,请重新显示表单返回View(model);}
问题描述非常简单:尝试使用种子用户的凭据登录失败.
具体地说,即使用户存在于数据库中, FindAsync
方法也返回 null
- FindByEmailAsync
确实找到了种子用户./p>
但是,创建一个新帐户可以使我登录.
即使我可以注册一个新帐户并使用该帐户登录,为什么也不能以种子用户身份登录?
我怀疑这与密码的散列方式有关,但我不知道如何确认.
我的帐户播种错误吗?我不应该在 Seed
方法中创建单独的 ApplicationUserManager
吗?如果没有,我应该如何获得一个才能调用 Create
?在试图将新系统锁定在我的帐户之外或将用户最终锁定在已部署的应用程序中之前,我试图了解新系统的工作原理.
以下代码:
var user =等待UserManager.FindAsync(model.Email,model.Password);
期望传入用户名,而不是电子邮件地址.
此简单的更改应注意以下事项:
var user =等待UserManager.FindAsync(model.UserName,model.Password);
I'm working on a new ASP.NET MVC project, using individual accounts stored in the database for authentication. Here's my class that will seed the database with sample data every time I test:
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();
}
}
The Login
action is as it is in the template:
//
// 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);
}
The description of problem is very simple: Trying to log in using the seeded user's credentials fails.
Specifically, the FindAsync
method returns null
, even though the user is present in the database - FindByEmailAsync
does find the seeded user.
However, creating a new account works and allows me to log in.
Why can't I log in as the seeded user, even though I can register a new account and log in using that?
I'm suspecting it has to do with how the passwords are hashed, but I don't know how to confirm this.
Am I seeding the account wrong? Should I not be creating a separate ApplicationUserManager
in the Seed
method? If not, how should I get one in order to call Create
? I'm trying to understand how the new system works, before ending up locked out of my account or the users end up locked out of theirs in a deployed application.
The following code:
var user = await UserManager.FindAsync(model.Email, model.Password);
is expecting the userName to be passed in, not the email address.
This simple change should take care of things:
var user = await UserManager.FindAsync(model.UserName, model.Password);
这篇关于为什么我不能以种子用户身份登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!