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

后端 未结 3 1227
难免孤独
难免孤独 2020-12-09 06:22

Using Visual Studio 2013.4 (Visual Studio 2013 Update 4) I have created a regular ASP.NET MVC 5 project with Individual User Accoun

相关标签:
3条回答
  • 2020-12-09 06:59
    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 讨论(0)
  • 2020-12-09 07:00

    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.

    0 讨论(0)
  • 2020-12-09 07:13

    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));
        }
    }
    
    0 讨论(0)
提交回复
热议问题