How to check if a user exists on the client side in ASP.NET MVC 5?

Using Visual Studio 2013.4 (Visual Studio 2013 4 Update) I created a regular ASP.NET MVC 5 project with separate authentication configuration user accounts . All user registration and login functions are already prepared for me by Visual Studio and work fine.

How to implement client-side verification of the following rule on the registration page: There is no already registered user with the same email address ?

+7
source share
3 answers

You can use RemoteAttribute to perform client-side validation using a server callback.

1) Add the following method to the AccountController :

 [AllowAnonymous] public async Task<JsonResult> UserAlreadyExistsAsync(string email) { var result = await userManager.FindByNameAsync(email) ?? await userManager.FindByEmailAsync(email); return Json(result == null, JsonRequestBehavior.AllowGet); } 

2) Add the Remote attribute to the Email property of the RegisterViewModel class:

 [Remote("UserAlreadyExistsAsync", "Account", ErrorMessage = "User with this Email already exists")] public string Email { get; set; } 

where "Account" is the name of the serving controller, and "UserAlreadyExistsAsync" is the name of the action.

+12
source

It has helped a lot. In my case, it was a table where updates were also possible. In this case, the indicated solution does not work. So I wanted to share my decision for this case.

In the solution below, I added an additional field to go to the controller (the main key of the model). Then in the controller I check to see if a primary key is set. If so, we know that we came from the update site, as this is the only case when we already have an identifier in the model. The final step is to check if the row and primary key are the same. If both of them, this is normal, because we did not change anything in the line. If only the string is the same but not the identifier, this means that we changed the string and changed it to another existing string, so we return false.

Model:

  [Key] [Display(Name = "Idee ID")] public int intIdeaID { get; set; } [Required(ErrorMessage = "Dieses Feld muss ausgefΓΌllt werden")] [Display(Name = "Idee")] [Remote("ideaExists", "TabIdea", HttpMethod = "POST", ErrorMessage = "Es wurde bereits eine Idee mit dieser Bezeichnung erstellt", AdditionalFields = "intIdeaID")] public string strIdea { get; set; } 

Controller:

 [HttpPost] public JsonResult ideaExists(string strIdea, int? intIdeaID) { if (intIdeaID != null) { if (db.tabIdea.Any(x => x.strIdea == strIdea)) { tabIdea existingTabIdea = db.tabIdea.Single(x => x.strIdea == strIdea); if (existingTabIdea.intIdeaID != intIdeaID) { return Json(false); } else { return Json(true); } } else { return Json(true); } } else { return Json(!db.tabIdea.Any(x => x.strIdea == strIdea)); } } 
0
source
 ASP.NET Core Identity is a membership system that adds login functionality to ASP.NET Core apps. Users can create an account with the login information stored in Identity or they can use an external login provider. **code is here** services.Configure<IdentityOptions>(options => { options.Password.RequiredLength = 8; options.User.RequireUniqueEmail = true; }); 
0
source

Source: https://habr.com/ru/post/982453/


All Articles