【问题标题】:Unique custom profile field asp.net identity MVC5唯一的自定义配置文件字段 asp.net 身份 MVC5
【发布时间】:2017-01-11 18:38:38
【问题描述】:

我是 MVC 新手,我正在为工作中的客户开发一个自助服务应用程序,他们将使用他们的客户端代码和密码登录,我使用带有 DBContext 的 asp.net 身份并设法创建了一个自定义配置文件字段是 ClientCode 并且工作正常,但我需要使该字段唯一,以便用户数据库中仅存在一个 ClientCode,我想也许在注册新帐户时检查这一点,不知道如何实现该约束;

My AccountController 注册方法

[HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email, ClientCode = model.ClientCode };

            var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return RedirectToAction("ClientsHome", "Home");
                }
                AddErrors(result);
        }
        // If we got this far, something failed, redisplay form
        return View(model);
    }

我的 AccountViewModel 注册ViewModel

   public class RegisterViewModel
{
    [Required]
    [Display (Name = "Client Code")]
    public long ClientCode { get; set; }

    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

【问题讨论】:

    标签: asp.net identity asp.net-mvc-5


    【解决方案1】:

    你可以做一些简单的事情:

    public bool ClientCodeExists(long clientCode)
    {
       //you will get values from your db. I've used this as a simple exmaple.
       List<long> clientCodesList = new List<long>();
    
       clientCodesList.Add(100);
       clientCodesList.Add(200);
    
       return clientCodesList.Any(a => a == clientCode);
    }
    

    然后在您的控制器中,您可以将其称为:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
           long clientCode = model.ClientCode;
           bool clientCodeExists = ClientCodeExists(clientCode);
    
           if(clientCodeExists)
           {
              //setup app user, create identity etc
           }
           else
           {
              ModelState.AddModelError("", "Error message you want to show to end user.");
           }
        }
    }
    

    【讨论】:

    • Thanx 这正是我正在寻找的但是我通过替换两行 ClientCodeList.Add(100), ClientCodeList.Add(200) 改变了将客户端代码添加到列表中的方式) 与 clientCodesList = context.Users.Select(u => u.ClientCode).ToList();但它仍然会创建重复的客户端代码
    • 好的,我终于设法通过更改为 if(clientCodeExists == false) 使其工作
    猜你喜欢
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    相关资源
    最近更新 更多