[英]Why can't I log in with the newly seeded power user (Asp.Net Core Identity)?
I have an Asp.Net Core 3.1 web app with Identity.我有一个带有身份的 Asp.Net Core 3.1 web 应用程序。 I use this code to seed a power user to the database:我使用此代码将高级用户播种到数据库:
var poweruser = new ApplicationUser
{
UserName = Configuration.GetSection("AppSettings")["UserName"],
Email = Configuration.GetSection("AppSettings")["UserEmail"],
FirstName = Configuration.GetSection("AppSettings")["UserFirstName"],
LastName = Configuration.GetSection("AppSettings")["UserLastName"],
PhoneNumber = Configuration.GetSection("AppSettings")["UserPhoneNumber"]
};
string userPassword = Configuration.GetSection("AppSettings")["UserPassword"];
var user = await UserManager.FindByEmailAsync(Configuration.GetSection("AppSettings")["UserEmail"]);
if (user == null)
{
var createPowerUser = await UserManager.CreateAsync(poweruser, userPassword);
if (createPowerUser.Succeeded)
{
await UserManager.AddToRoleAsync(poweruser, "SiteAdmin");
}
}
In appsettings.json
, the section "AppSettings" looks like this:在appsettings.json
中,“AppSettings”部分如下所示:
"AppSettings": {
"UserName": "ab@cd.ef",
"UserEmail": "ab@cd.ef",
"UserFirstName": "Admin",
"UserLastName": "Admin",
"UserPhoneNumber": "12345678",
"UserPassword": "pa5sw&Rd"
}
The user is created in the database:用户在数据库中创建:
But when I try to log in, I get a failed login attempt.但是当我尝试登录时,登录尝试失败。 I have seen this and this similar post, but in those cases, the username and email address were not identical.我看过这个和这个类似的帖子,但在这些情况下,用户名和 email 地址并不相同。 In my case, they are (ab@cd.ef).就我而言,它们是(ab@cd.ef)。 I copy-pasted the password from appsettings.json to make sure I wasn't mistyping.我从 appsettings.json 复制粘贴了密码,以确保我没有输入错误。
Update更新
This is the method receiving the login form (Login.cshtml.cs):这是接收登录表单(Login.cshtml.cs)的方法:
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
if (ModelState.IsValid)
{
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
_logger.LogInformation("User logged in.");
return LocalRedirect(returnUrl);
}
if (result.RequiresTwoFactor)
{
return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
}
if (result.IsLockedOut)
{
_logger.LogWarning("User account locked out.");
return RedirectToPage("./Lockout");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return Page();
}
}
// If we got this far, something failed, redisplay form
return Page();
}
Don't you hate it when the solution to a difficult problem turns out to be simple?当一个难题的解决方法变得很简单时,你不讨厌它吗?
In this case, all it took was to set PhoneNumberConfirmed
to true
, and I was able to log in...在这种情况下,只需将PhoneNumberConfirmed
设置为true
,我就可以登录了......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.