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

▼魔方 西西 提交于 2019-11-28 08:43:38

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.

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));
    }
}
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;
            });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!