问题
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.
Here are the edits to the Register.cshtml.cs page.
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; }
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 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.
回答1:
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 .
Create new
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); } }Register the
SignInManager:services.AddDefaultIdentity<IdentityUser>() .AddDefaultUI(UIFramework.Bootstrap4) .AddSignInManager<MySignInManager>() //register new SignInManager .AddEntityFrameworkStores<ApplicationDbContext>();
来源:https://stackoverflow.com/questions/55620595/i-need-to-save-the-user-name-and-email-address-separately-i-am-am-having-an-iss