唯一自定义配置文件字段 asp.net 标识 MVC5
Unique custom profile field asp.net identity MVC5
我是 MVC 的新手,我正在为工作中的客户开发一个自助服务应用程序,他们将使用他们的客户代码和密码登录,我使用 asp.net 身份和 DBContext 并设法创建一个自定义配置文件字段,它是 ClientCode 并且工作正常,但我需要使该字段唯一,以便用户数据库中只存在一个 ClientCode,我想也许可以在注册新帐户时检查它,但不确定如何实现该约束;
我的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; }
}
您可以做一些简单的事情:
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.");
}
}
}
我是 MVC 的新手,我正在为工作中的客户开发一个自助服务应用程序,他们将使用他们的客户代码和密码登录,我使用 asp.net 身份和 DBContext 并设法创建一个自定义配置文件字段,它是 ClientCode 并且工作正常,但我需要使该字段唯一,以便用户数据库中只存在一个 ClientCode,我想也许可以在注册新帐户时检查它,但不确定如何实现该约束;
我的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; }
}
您可以做一些简单的事情:
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.");
}
}
}