Suggested Videos
Part 72 - Redirect user to original url after login in asp.net core | Text | Slides
Part 73 - Open redirect vulnerability example | Text | Slides
Part 74 - ASP.NET Core client side validation | Text | Slides
In this video we will discuss Remote Validation in ASP.NET Core with an example.
What is Remote Validation in ASP.NET Core
Remote validation allows a controller action method to be called using client side script. This is very useful when you want to call a server side method without a full page post back.
Remote validation example
Consider the following User Registration Page
Checking if the provided email is already taken by another user can only be done on the server. The following IsEmailInUse() controller action method checks if the provided email is in use.
The following is the model class for the User Registration View. Notice, we have decorated the Email property with the [Remote] attribute pointing it to the action method that should be invoked when the email value changes.
ASP.NET core remote validation not working
The following 3 client-side libraries are required in the order specified for the remote validation to work. If any of them are missing or not loaded in the order specified, remote validation will not work.
Part 72 - Redirect user to original url after login in asp.net core | Text | Slides
Part 73 - Open redirect vulnerability example | Text | Slides
Part 74 - ASP.NET Core client side validation | Text | Slides
In this video we will discuss Remote Validation in ASP.NET Core with an example.
What is Remote Validation in ASP.NET Core
Remote validation allows a controller action method to be called using client side script. This is very useful when you want to call a server side method without a full page post back.
Remote validation example
Consider the following User Registration Page
Checking if the provided email is already taken by another user can only be done on the server. The following IsEmailInUse() controller action method checks if the provided email is in use.
[AcceptVerbs("Get", "Post")]
[AllowAnonymous]
public async Task<IActionResult> IsEmailInUse(string email)
{
var user = await
userManager.FindByEmailAsync(email);
if (user == null)
{
return Json(true);
}
else
{
return Json($"Email {email} is already in
use.");
}
- This method should respond to both HTTP GET and POST. This is the reason we specified both the HTTP verbs (Get and Post) using [AcceptVerbs] attribute.
- ASP.NET Core MVC uses jQuery remote() method which in turn issues an AJAX call to invoke the server side method.
- The jQuery remote() method expects a JSON response, this is the reason we are returning JSON response from the server-side method (IsEmailInUse)
The following is the model class for the User Registration View. Notice, we have decorated the Email property with the [Remote] attribute pointing it to the action method that should be invoked when the email value changes.
public class RegisterViewModel
{
[Required]
[EmailAddress]
[Remote(action: "IsEmailInUse", controller: "Account")]
public string Email { get; set; }
//
Other properties
}
ASP.NET core remote validation not working
The following 3 client-side libraries are required in the order specified for the remote validation to work. If any of them are missing or not loaded in the order specified, remote validation will not work.
<script src="~/lib/jquery/jquery.js"></script>
<script src="~/lib/jquery-validate/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
I don't why but after executing this line await userManager.FindByEmailAsync(email), I do not get response. I waited for a long time.
ReplyDeleteThe following 3 client-side libraries are required in the order specified for the remote validation to work. If any of them are missing or not loaded in the order specified, remote validation will not work.
Deletejquery.js
jquery-validate
jquery-validation-unobtrusive
@VIKAS SINGH
ReplyDeleteHappened the same to me.
To solve it all you have to do is foreach(var user in userManager.Users)
and if user.Email == Email just return Json(data: $"Email {Email} is already in use");