How to check if user already exists on client-side

2019-01-18 00:06发布

问题:

Using Visual Studio 2013.4 (Visual Studio 2013 Update 4) I have created a regular ASP.NET MVC 5 project with Individual User Accounts authentication configuration. All the users registration and log-in features has been already scaffolded for me by Visual Studio and works fine.

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

回答1:

You could use RemoteAttribute to perform client side validation with 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 Remote attribute to Email property of 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 it's action name.



回答2:

This helped alot. In my case it was a table, where updates were also possible. In this case the above solution doesn't work. So I wanted to share my solution for this case.

In the solution below, I added an additional field to pass to the Controller(The Primary Key of the Model). Then in the controller I am checking if the Primary Key is given. If so, we know, that we came from the update site since it's the only case where we already have an ID in the model. The last step is to check if the string and the primary key is the same. If they both are, it's ok, because we didn't change anything in the string. If only the string is the same but not the ID, it means, that we changed the string and changed it to another existing items 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));
    }
}