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?

4
  • Do you have two input fields in UI for Username and Email? Commented Nov 16, 2021 at 6:54
  • Yes, the required elements (UserName, Email, Password) are available in the Login.cshtml file in the View layer of MVC. Commented Nov 16, 2021 at 8:05
  • Are all these fields mandatory? or Username OR Email AND Password Commented Nov 16, 2021 at 8: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. Commented Nov 16, 2021 at 9:19

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.