1

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?

Sercan
  • 4,739
  • 3
  • 17
  • 36
  • Do you have two input fields in UI for Username and Email? – Anuraj Nov 16 '21 at 06:54
  • Yes, the required elements (`UserName`, `Email`, `Password`) are available in the **Login.cshtml** file in the View layer of MVC. – Sercan Nov 16 '21 at 08:05
  • Are all these fields mandatory? or Username OR Email AND Password – Anuraj Nov 16 '21 at 08:14
  • As I mentioned in the question, the `Password` will be a required field. Only one of the `UserName` or `Email` information will be mandatory. What I want to ask here is an **"OR"** relationship where one of the two fields (`UserName`, `Email`) will be accepted but the preference will be determined by the user at login. – Sercan Nov 16 '21 at 09:19

1 Answers1

2

You need to implement a custom validator. When you implement a Custom Validator, you can decide the logic and it can be implemented in both server-side and client-side. And ASP.NET validation infrastructure takes care of validating it along with the other validators.

Here is the doc which talks about implementing custom validation

Here is one example - https://stackoverflow.com/a/15975880/38024 which will help you. Or you can use external nuget packages like ExpressiveAnnotations

Anuraj
  • 18,859
  • 7
  • 53
  • 79
  • I didn't know that a custom validator could be defined. It can solve my requirements by defining custom validator. Thank you for your help. – Sercan Nov 16 '21 at 11:35