简体   繁体   English

如何检查ASP.NET MVC 5中客户端是否已存在用户?

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

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. 使用Visual Studio 2013.4 (Visual Studio 2013 Update 4)我创建了一个具有个人用户帐户身份验证配置的常规ASP.NET MVC 5项目。 All the users registration and log-in features has been already scaffolded for me by Visual Studio and works fine. 所有用户注册和登录功能已经由Visual Studio支持我,并且工作正常。

How to implement client-side validation of the following rule on the registration page: There is no already registered user with the same Email ? 如何在注册页面上实现以下规则的客户端验证: 没有已注册的用户使用相同的电子邮件

You could use RemoteAttribute to perform client side validation with a server callback. 您可以使用RemoteAttribute通过服务器回调执行客户端验证。

1) Add the following method to the AccountController : 1)将以下方法添加到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: 2)将Remote属性添加到RegisterViewModel类的Email属性:

[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. 其中"Account"是服务控制器的名称, "UserAlreadyExistsAsync"是它的动作名称。

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). 在下面的解决方案中,我添加了一个附加字段以传递给Controller(模型的主键)。 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. 如果是这样,我们知道,我们来自更新站点,因为它是我们在模型中已经有ID的唯一情况。 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. 如果只有字符串相同但不是ID,则意味着我们更改了字符串并将其更改为另一个现有项字符串,因此我们返回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;
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM