Use Username instead of Email for identity in Asp.net mvc5

35,572

Solution 1

The linked question in the accepted answer descibes how to use Email instead of UserName, OP wanted the UserName instead of email which is what I was looking for in Identity 2.0 in an MVC project.

In case anyone else gets here from a google search it is actually very easy to do this. If you look at the register post action it is simply setting the UserName to the Email address. So.........

Add UserName to the RegisterViewModel and add it to the register view.

<div class="form-group">
        @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.UserName, new { @class = "form-control", @placeholder = "Username or email" })
        </div>
</div>

In the Register Post Action on the AccountController set the UserName to the ViewModel's UserName and the Email to the ViewModel's Email.

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

Solution 2

In order to make email as non unique:

Configure below in IdentityConfig

manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = false                
            };
Share:
35,572
ibnhamza
Author by

ibnhamza

Updated on August 28, 2020

Comments

  • ibnhamza
    ibnhamza over 3 years

    Whenever I create a new application with visual studio 2013 express for web and using the individual accounts authentication and i hit the register button I notice that it implements 'Email' instead of 'Username' and the same is in the LoginViewModel as it uses Email to Sign in instead of Username. How can i change this to use Username instead of the default Email without trouble? Also i would like to know how to convert the default 'guid' that is a string type to 'id' (integer type).

  • Yauvaraj Rimal
    Yauvaraj Rimal about 9 years
    I don't want to use email in any way. Is there any way that I can register without providing email address?
  • Hoody
    Hoody about 9 years
    I don't know if the email address is mandatory in the model but if it's not you could remove the four lines in the registration view for the email address and replace them with the UserName code I have listed above. Then you could set the new ApplicationUser as follows in the post action in your AccountController. var user = new ApplicationUser { UserName = model.UserName, Email = model.Email }; If the email address is mandatory just set the email to string.empty
  • Xipooo
    Xipooo over 8 years
    This was the simplest solution for me and got rid of one issue I had after converting from MVC 4 to MVC 5.
  • Brian P
    Brian P almost 8 years
    Validation checks for string.Empty. sasi kumar's answer works correctly.
  • TheQult
    TheQult about 7 years
    made the same, but registration doens't work because Username in RegisterViewModel is still null
  • Hamza Khanzada
    Hamza Khanzada about 4 years
    You are a champ man! saved my day. This should be the accepted answer