Replacing Email with a normal Username in ASP.NETCore UserIdentity

We use ASP.NETCore UserIdentity a lot to implement and manage the User authentication mechanism for your website or application. Many times we don’t want the username field to be a valid email address and we don’t want to kick off the email confirmation process when we create user logins. This particularly makes sense when the logins are not associated with an email address and the administrator don’t care about users confirming via email every time a user is created/registered due to various reasons. Note that we are basing our coding reference to .NETCore 3.1. Ok, here is how we do it –

Step 1: Add UserName to the Register InputModel in Register.cshtml.cs

Before you do anything make sure you scaffold UserIdentity to your project (check the net on how to do this). Once you do that add below to your Register.cshtml.cs above the Email field.

            [Required]
            [Display(Name = "Username")]
            [StringLength(128)]
            public string UserName { get; set; }

Step 2: Switch off email confirmation process and set EmailConfirmed true in Register.cshtml.cs

2.1 Comment out below line to switch off email confirmation

                     //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    //var callbackUrl = Url.Page(
                    //    "/Account/ConfirmEmail",
                    //    pageHandler: null,
                    //    values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                    //    protocol: Request.Scheme);

                    //await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                    //    $"Please confirm your account by clicking here.");

This will make sure the registration process will not kick off email confirmation process.

2.2 Set EmailConfirmed to true since we are bypassing above step in Register.cshtml.cs

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

Step 3 Add UserName to the Registration Page in Register.cshtml

Add below to Register.cshtml –

            <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>

Step 4: Add UserName to the Login InputModel in Login.cshtml.cs

Add below to your Login.cshtml.cs above the Email field.

            [Required]
            [Display(Name = "Username")]
            [StringLength(128)]
            public string UserName { get; set; }

Step 5: Pass Username for authentication in Login.cshtml.cs

var result = await _signInManager.PasswordSignInAsync(Input.UserName, Input.Password, Input.RememberMe, lockoutOnFailure: false);

Last Step: Set RequireConfirmedAccount flag in Startup.cs to false

services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = false)

That’s it! Enjoy!