Search code examples
asp.net-corerazor-pages

How can I check in the login whether the email exists in a database - razor page?


I want the site login form to check if the email is in the system or not, to return an error message. I can not do this because I am new. I tried to do something I think that's the direction, I'd be happy if you could help me.

public class ConnectionAttribute : ValidationAttribute
    {

        private readonly OrderContext _context;

        private readonly string _connectingMail;
        public ConnectionAttribute(string connectingMail)
        {
            _connectingMail = connectingMail;
        }

        protected override ValidationResult IsValid(
       object value, ValidationContext validationContext)
        {
            List<Customer> allCustomers = _context.Customer.ToList();
            List<string> allMails = new List<string>();
            foreach (var item in allCustomers)
            {
                allMails.Add(item.CustomerMail);
            }
            var file = value as string;
            if (allMails.Contains(_connectingMail))
            {
                    return new ValidationResult(GetErrorMessage());
             
            }

            return ValidationResult.Success;
        }

        public string GetErrorMessage()
        {
            return $".";
        }

    }

I think this code is correct, but I do not know what to call it after clicking connect . What can you call him?


Solution

  • You could try to use Remote validation.

    First, Remote validation in ASP.NET (Core) relies on Unobtrusive AJAX, so you will need to install that first. The easiest way to do this is via LibMan. Please check the wwwroot/lib folder whether you have installed them or not. If doesn't install it, refer the following steps:

    Please right click on the lib folder in wwwroot, choose Add » Client-side Library, and then choose jsdelivr as the source, and type in jquery-ajax-unobtrusive.

    Second, since your application is an Asp.net Core Razor application, in the PageModel, add Email property with PageRemote attribute and a OnPostCheckEmail method to check whether the email is exist or not:

    public class RemoteValidateModel : PageModel
    {
        [PageRemote(ErrorMessage = "Email Address already exists", AdditionalFields = "__RequestVerificationToken",
            HttpMethod = "post",PageHandler = "CheckEmail")]
        [BindProperty]
        public string Email { get; set; }
        public void OnGet()
        {
        }
        //this method is used to check whether the email is exist or not.
        public JsonResult OnPostCheckEmail()
        {
            //query the database and get all existing Emails or directly check whether the email is exist in the database or not.
            var existingEmails = new[] { "jane@test.com", "claire@test.com", "dave@test.com" };
            var valid = !existingEmails.Contains(Email);
            return new JsonResult(valid);
        }
    }
    

    Code in the Razor Page:

    @page
    @model RazorSample.Pages.RemoteValidateModel
    @{
    }
    <form method="post">
        <input asp-for="Email" />
        <span asp-validation-for="Email" class="text-danger"></span><br>
        <input type="submit" />
    </form>
    @section scripts{
        <script src="~/lib/jquery/dist/jquery.min.js"></script>
        <partial name="_ValidationScriptsPartial" />
        <script src="~/lib/jquery-ajax-unobtrusive/dist/jquery.unobtrusive-ajax.min.js"></script>
    }
    

    Then, the result as below:

    enter image description here

    More detail information, please check this tutorial:

    Remote Validation in Razor Pages

    Besides, if you want to use Remote validation in Asp.net Core MVC application, you can check [Remote] attribute.