Reputation: 35
I'm new to MVC and I am developing a self service application for our clients at work which they will login to using their clientcode and password, I used asp.net identity with DBContext and managed to create a custom profile field which is ClientCode and is working fine but I need to make this field unique such that only one ClientCode exists in the users database, I was thinking maybe to check this on registering a new account, not sure how I can implement that constraint;
My AccountController Register method
[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);
}
My 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; }
}
Upvotes: 0
Views: 121
Reputation: 6866
You could do something as simple as:
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);
}
Then in your controller you can call this as:
[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.");
}
}
}
Upvotes: 1