question

sercan avatar image
0 Votes"
sercan asked BrandoZhang-MSFT commented

Being Able To Login Using UserName or Email address in ASP.NET Core MVC

I want user to be able to login using UserName or Email in ASP.NET Core MVC. In case of using the [Required] attribute, both the UserName and the Email must be entered in the model properties during login. While doing the form validation on the server-side, I need to perform model validation before accessing the data access layer in the application where the N-Tier architecture is implemented.


 namespace LoginApplication.WebUI.Models
 {
     public class LoginModel
     {
         /// <summary>Which data annotation attribute should I use?</summary>
         public string UserName { get; set; }
    
         /// <summary>Which data annotation attribute should I use?</summary>
         public string Email { get; set; }
    
         [Required]
         [DataType(DataType.Password)]
         public string Password { get; set; }
     }
 }
    
 namespace LoginApplication.WebUI.Controllers
 {
     public class AccountController : Controller
     {
      /* Other LOCs: Dependency Injection, "Login" Action HttpGet Method */
    
         [HttpPost]
         public async Task<IActionResult> Login(LoginModel model)
         {
             /* At this stage I need to validate the model correctly. */
             if(ModelState.IsValid)
             {
                 /* Other LOCs: User Query, Password Authentication, Page Redirection
                                Error Message Management, Log Management */
             }
             return View(model);
         }
     }
 }

In this case, what attribute should I use defined in the System.ComponentModel.DataAnnotations namespace? Or is there another way to work around this situation?

dotnet-aspnet-core-generaldotnet-aspnet-core-mvcdotnet-aspnet-core-auth
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Use the [Required] attribute for the required model properties.

 [Required]
 public string UserName { get; set; }
    
 [Required]
 public string Email { get; set; }

Reference documentation.
Model validation in ASP.NET Core MVC and Razor Pages
ASP.NET Core MVC with EF Core - tutorial series


0 Votes 0 ·

I want to login using UserName "or" Email address according to the user's request. As you stated, both UserName and Email address must be entered mandatory when the [Required] attribute is used for both properties. Otherwise, the validation will be deemed invalid.

0 Votes 0 ·

I don't agree with the design and find it rather confusing for the user. A possible design approach is adding one more input, like a checkbox, that lets the user pick if the email input is also the username. If the checkbox is checked then the username and email inputs must be equal.

0 Votes 0 ·
Show more comments

1 Answer

Bruce-SqlWork avatar image
1 Vote"
Bruce-SqlWork answered BrandoZhang-MSFT commented

Just allow the user to use their email as username. Trivial with a little javascript and a checkbox.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the suggestion. I learned that this requirement can be fulfilled by defining custom validator.

0 Votes 0 ·

Hi sercan,

Yes, your requirement could be done by using custom validate. The next questions is this custom validate is server validation, it happened at the server-side. Client side will not work if you used this custom validate.

1 Vote 1 ·

I'm using the jquery-validate plugin to check the form on the client-side without going to the server-side. The jquery-validate plugin includes a feature that allows some entries to be ignored:

 $("#myForm").validate({
    ignore: ".ignore"
 })

Client-side validation can be blocked in a single field:

  $("#field").rules('remove', 'required');
1 Vote 1 ·
Show more comments