Login with email to MVC 5

In a newly created ASP.NET MVC5 application, the login screen prompts you to enter an email address. But under the hood, it authenticates with a username that assumes applicationUser.UserName

== applicationUser.Email

.

How do I change it so that it authenticates the user via email?

It looks like they changed something in Identity 2.x in this regard.

Note. I want him to register by email.

For example, in the registry view, our users must provide:

Email: last.first@example.com

User: Last, First

And an email will be used during login. It currently uses email, but authentication fails as it passes the email address as the username.

+3


source to share


2 answers


In Login # Accounts:

After

if (!ModelState.IsValid) 
{ 
    return View(model); 
} 

      

I added:

var user = await UserManager.FindByEmailAsync(model.Email);

      



And changed:

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); 

      

in

var result = user == null ? SignInStatus.Failure : await SignInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, shouldLockout: false); 

      

In registration mode, I have a username as well as an email address (for which I had to add a property UserName

to RegisterViewModel

).

+5


source


I had the same problem with the identity system. In an earlier version, the email address was not part of the registration process. And an identity implementation that validates the username as unique and authenticates users against the username and password. But allowed us to manage and update the "IUserStore" properties to support unique emails and the username to be able to insert an email address (not AlphanumericUserNames). But that was not in the visual studio MVC individual user accounts template that we have to add to ourselves.

The code we need to add to ApplicationUserManager.cs, sample code included in the following NuGet package.

Installation Package Microsoft.AspNet.Identity.Samples -Version 2.0.0-beta2 -Pre

    manager.UserValidator = new UserValidator<ApplicationUser>(manager)
    {
        AllowOnlyAlphanumericUserNames = false,
        RequireUniqueEmail = true
    };

      



In version 2.x, the Identity Framework included an email field as a unique field and also added all the required codes to the MVC visual studio individual user accounts template in App_Start -> IdentityConfig. In addition, we removed part of the username from the UI and only saved it on the server.

But the authentication mechanism hasn't changed and it still uses the username and password for verification. This change only happened during user registration, the email address will be inserted into the username field that is used when authenticating users.

In my opinion, there is no identity framework support for authenticating users with email and password at the moment. You have to insert email in the username field when registering users.

0


source







All Articles