独特的自定义配置文件字段asp.net身份MVC5

时间:2017-01-11 09:47:52

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

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

我的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 RegisterViewModel

   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; }
}

1 个答案:

答案 0 :(得分: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.");
       }
    }
}