我需要分别保存用户名和电子邮件地址.注册用户后,我在登录网站时遇到问题 [英] I need to save the User Name and email address separately. I am am having an issue with logging into the site after the user is registered

查看:61
本文介绍了我需要分别保存用户名和电子邮件地址.注册用户后,我在登录网站时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个新的ASP.NET Core(2.2)网站.默认情况下,用户名和电子邮件地址是同一个人.我想让他们与众不同.当我尝试将该用户重新登录到网页时,就会出现此问题.

I am creating a new ASP.NET Core (2.2) WebSite. By default, the UserName and Email Address is the same thing. I want to have them be different. The issue comes in when I try to log that user back into the web page.

我架设了寄存器身份"页面,并对后面的代码"以及剃刀"页面本身进行了一些简单的更改(请参见下文).我确实也搭建了登录页面,但是我认为不需要在此处进行任何更改,因为我同意人们仍使用他们的电子邮件地址登录.

I scaffolded the register Identity page and made some simple changes to the Code behind and also the Razor page itself (see below). I did also scaffold the login page, but I don't think that I need to make any changes there, because I am ok with folks logging in using their email address still.

这是对Register.cshtml.cs页面的编辑.

Here are the edits to the Register.cshtml.cs page.

我将其添加到InputModel类中

I added this to the InputModel class:

[Required]
[StringLength(256, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 5)]
[DataType(DataType.Text)]
[Display(Name = "User Name")]
public string UserName { get; set; }

我还通过OnPostAsync方法更改了用户

I also changed the user in the OnPostAsync method

var user = new IdentityUser { UserName = Input.UserName, Email = Input.Email };

所有其他设置均为默认设置.

Everything else is as default.

在页面本身上,我只是将其添加到电子邮件下面:

On the page itself, I simply added it below the email:

<div class="form-group">
    <label asp-for="Input.UserName"></label>
    <input asp-for="Input.UserName" class="form-control"/>
    <span asp-validation-for="Input.UserName" class="text-danger"></span>
</div>

I,我希望用户能够正常登录.我得到的错误仅仅是无效的登录尝试".烦人的是,我在进行任何更改之前创建的用户仍然可以登录.所以我的直觉告诉我,在注册页面上有什么好笑的,我只是不确定在哪里.

I would expect for the user to be logged in as normal, alas. The error I get is simply "Invalid Log-in attempt". Annoyingly, The user I created before making any changes still is able to be logged in just fine. So my gut tells me that Something is funny in the register page, I am just not sure where.

推荐答案

您必须修改 SignInManager.PasswordSignIn 方法.默认情况下,它使用 FindByNameAsync 来检查给定名称的用户是否存在,您应该更改为 FindByEmailAsync .

You have to modify SignInManager.PasswordSignIn method. By default it uses FindByNameAsync to check if user with given name exists , you should change to FindByEmailAsync .

  1. 创建新的 SignInManager :

public class MySignInManager : SignInManager<IdentityUser>
{
    public MySignInManager(Microsoft.AspNetCore.Identity.UserManager<Microsoft.AspNetCore.Identity.IdentityUser> userManager, Microsoft.AspNetCore.Http.IHttpContextAccessor contextAccessor, Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory<Microsoft.AspNetCore.Identity.IdentityUser> claimsFactory, Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.Identity.IdentityOptions> optionsAccessor, Microsoft.Extensions.Logging.ILogger<Microsoft.AspNetCore.Identity.SignInManager<Microsoft.AspNetCore.Identity.IdentityUser>> logger, Microsoft.AspNetCore.Authentication.IAuthenticationSchemeProvider schemes)
        : base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemes)
    {
    }

    public override async Task<SignInResult> PasswordSignInAsync(string userName, string password,
        bool isPersistent, bool lockoutOnFailure)
    {
        var user = await UserManager.FindByEmailAsync(userName);
        if (user == null)
        {
            return SignInResult.Failed;
        }

        return await PasswordSignInAsync(user, password, isPersistent, lockoutOnFailure);
    }
}

  • 注册 SignInManager :

    services.AddDefaultIdentity<IdentityUser>()
        .AddDefaultUI(UIFramework.Bootstrap4)
        .AddSignInManager<MySignInManager>()   //register new SignInManager 
        .AddEntityFrameworkStores<ApplicationDbContext>();
    

  • 这篇关于我需要分别保存用户名和电子邮件地址.注册用户后,我在登录网站时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆