ValidationAttribute RRS feed

  • Question

  • Hi All,

    I am using VS 2017 core 2.1, and I want to verify a users login credentials. We have an administrative MVC application. This application will store user credential information in a database if the user is authorized (several conditions, but in my example below, I have included two conditions to keep it simple for this question). So in the model, I have created a user email and password. I then want to check the credentials before storing those credentials in the database.

    I have written the following example class derived from ValidationAttribute. 


    public class Class1
        public sealed class MyCredentialCheck : ValidationAttribute
            public string email { get; set; }
            public string password { get; set; }
            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
                if (string.IsNullOrEmpty(email))
                    return new ValidationResult("Invalid email address");
                if (string.IsNullOrEmpty(password))
                    return new ValidationResult("Invalid password");
                if(!VerifyCredentials(email, password))
                    return new ValidationResult("Not authorized");
                return ValidationResult.Success;

    So I am confused with where to implement this validation. I wanted to validate this in the view before the submit button.

    Does anybody have any suggestions on where to perform this validation? In the model or view? The controller will store the result in the database but want to efficiently block this if the credentials are bad.

    Thanks, Tom

    • Moved by CoolDadTx Monday, October 29, 2018 7:19 PM ASP.NET related
    Monday, October 29, 2018 5:25 PM

All replies

  • Please post questions related to MVC in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, October 29, 2018 7:19 PM