简体   繁体   English

为什么我不能用新播种的超级用户(Asp.Net Core Identity)登录?

[英]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.

相关问题 使用ASP.net核心Identity MVC登录用户的日志IP - Log IP of signed in user with ASP.net core Identity MVC 我想为 ASP.NET 核心标识中的用户创建一个帐户,但我无法将数据插入表 dbo.AspNetUsers - I want to create an account for a user in ASP.NET Core identity but I can't insert the data into table dbo.AspNetUsers 为什么我的asp.net身份-user会自动注销 - Why my asp.net identity -user will log out automatically 当我尝试使用 ASP.NET 核心标识创建新用户时,为什么我的 ApiKey 变量位于 null 中? - Why can it be that my ApiKey variable is in null when I try to create a new user using ASP.NET Core Identity? 如何使用ASP.NET Core Identity v Preview 3.0创建自定义用户和角色? - How i can create custom user and role with ASP.NET Core identity v preview 3.0? 如何在 ASP.NET 核心标识中检索用户的 2FA 恢复代码? - How can I retrieve 2FA recovery codes for a user in ASP.NET Core Identity? ASP.NET 核心。 如何在没有身份用户帐户的情况下创建身份验证 cookie? - ASP.NET Core. How can i create an auth cookie without an identity user account? 扩展ASP.NET Core身份用户 - extend ASP.NET Core Identity user ASP.NET Core Identity 用户组 - ASP.NET Core Identity user groups Asp.net身份无法添加用户角色 - Asp.net identity can't add user to role
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM