【发布时间】: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