21

I need to set my login to use username instead of email address, how can I change it?

Kevin Maxwell
  • 907
  • 2
  • 19
  • 38

2 Answers2

25

It's actually using the e-mail address as the username, so in the ASPNetUsers table, you'll see both the username and email fields with the email address.

Go into the AccountController, look for Register method (POST).

Change this:

var user = new ApplicationUser { UserName = model.Email, Email = model.Email};

to this:

var user = new ApplicationUser
            {
                UserName = model.UserName,
                Email = model.Email
            };

Then go into the Login.cshtml and change all corresponding e-mail model fields to username instead.

Finally, go into the Login method (POST) in the AccountController and change model.Email to model.UserName.

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

You also have to make changes in AccountViewModels.cs in order to introduce your new UserName property.

Kevin Maxwell
  • 907
  • 2
  • 19
  • 38
Jason Roner
  • 865
  • 1
  • 9
  • 14
  • 2
    Thanks Jason. The problem is when I type `model.UserName` I get the red underline and even when I type `model.` I don't see the UserName option to select. – Kevin Maxwell Oct 11 '15 at 20:52
  • @kevinMaxwell if you decide to use username, why dont you just modify ApplicationUser Model ? – Sakthivel Oct 12 '15 at 07:50
  • @KevinMaxwell nah, The application user model – Sakthivel Oct 12 '15 at 07:55
  • @codebrain I don't have any model under this name. :( – Kevin Maxwell Oct 12 '15 at 07:56
  • 5
    @codebrain Go into the AccountViewModels under the Models folder, look for LoginViewModel and you'll need to add a UserName property in there. You'll also have to do the same to the RegisterViewModel. It's definitely a lot to take in but practice is key! – Jason Roner Oct 12 '15 at 21:33
  • 2
    @KevinMaxwell In RegisterViewModel.cs change the property `Email` to `UserName` , remove the [EmailAddress] annotation from there and change the `[Display(Name = "Email")]` to `[Display(Name = "Login")]` or something you want to display. If you want to keep `Email` property, then add `UserName` property to the same view model and make it as required. – nam Aug 08 '16 at 15:55
  • I did what you wrote here, but I am receiving the message "Email cannot be null or empty." even if it is not in the form. I removed the attribute of Required from the RegisterViewModel and the AccountViewModel. Any ideas? – Exel Gamboa Dec 23 '16 at 23:43
  • 1
    I answered myself accidentally ... I saw something that reads "RequireUniqueEmail = true" in the manager.UserValidator in ApplicationUserManager inside the class ApplicationUserManager in the IdentityConfig.cs. Change that to "false" and now I can complete the registration. – Exel Gamboa Dec 24 '16 at 00:07
2

Here is how you do it

 var user = await _userManager.Users
           .FirstOrDefaultAsync(u => u.UserName == username || u.Email == username); 
 if (user != null){
    var result = await _signInManager
                .PasswordSignInAsync(/*email*/user.Email, password, false, false);
    /*removed for brevity*/
 }

Think you have a user having username=test and email=myemail@example.com then you would like to allow users to authenticate using test instead of myemail@... `

PS. While the answer from @Jason works, sometimes you'd like to authenticate a user with real username and password, not using the email.

In this case use my answer

Bellash
  • 7,560
  • 6
  • 53
  • 86