我有一个登录问题,即使我看到表中填满了我的种子用户信息从Configuration.cs:
protected override void Seed(www.Models.ApplicationDbContext context)
{
if (!context.Roles.Any(x => x.Name == "admin"))
{
var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
var role = new IdentityRole { Name = "admin" };
roleManager.Create(role);
}
if (!context.Users.Any(x => x.UserName == "admin" && x.Email == "admin@admin.com"))
{
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
var user = new ApplicationUser { UserName = "admin", Email = "admin@admin.com" };
var hasher = new PasswordHasher();
userManager.Create(user, "MySecret5");
userManager.AddToRole(user.Id, "admin");
}
}
,当我尝试登录我得到错误"无效登录尝试"。我错过了什么?
编辑:我在学习所有关于asp.net的东西,所以我现在相当大的新手:(所以我发现这个例子是为我工作,如果有人需要它在这里:
protected override void Seed(www.Models.ApplicationDbContext context)
{
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
if (!context.Users.Any(x => x.UserName == "admin@v.com"))
{
var user = new ApplicationUser { UserName = "admin@v.com", Email = "admin@v.com" };
userManager.Create(user, "Password5%");
context.Roles.AddOrUpdate(x => x.Name, new IdentityRole { Name = "admin" });
context.SaveChanges();
userManager.AddToRole(user.Id, "admin");
}
}
谢谢你的帮助和时间。
似乎与种子用户登录的问题与MVC不准确有关,请查看:
var result = await SignInManager.PasswordSignInAsync(model.Emali, model.Password, model.RememberMe, shouldLockout: false);
:
var user = new ApplicationUser { UserName = "SomeName", Email = "admin@v.com" };
结果将== false,但是如果我们改变模型。Emali中的结果到模型。用户名然后登录成功-当然现在当我们登录时,我们必须提供用户名,例如:
用户名:SomeName;
Passwort: Password5%;
这对我来说是完美的。
protected override void Seed(www.Models.ApplicationDbContext context)
{
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
if (!context.Users.Any(x => x.UserName == "admin@v.com"))
{
var user = new ApplicationUser { UserName = "admin@v.com", Email = "admin@v.com" };
userManager.Create(user, "Password5%");
context.Roles.AddOrUpdate(x => x.Name, new IdentityRole { Name = "admin" });
context.SaveChanges();
userManager.AddToRole(user.Id, "admin");
}
}