2017-01-11 13 views
0

私はMVCを初めて使い、クライアントコードとパスワードを使用してログインする私たちのクライアント用のセルフサービスアプリケーションを開発しています。私はasp.net IDをDBContext ClientCodeのカスタムプロファイルフィールドを作成して正常に動作していますが、このフィールドを独自のものにして、ユーザーデータベースにClientCodeが1つしかないようにする必要があります。私はその制約を実装できます。独自のカスタムプロファイルフィールドasp.net ID MVC5

マイ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

あなたは、ような単純なもの行うことができます:次に、あなたのコントローラーにあなたがこれを呼び出すことができます

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."); 
     } 
    } 
} 
+0

これはまさに私が探しているものですが、クライアントコードをClientCodeList.Add(100)、ClientCodeList.Add(200)の2つの行に置き換えてクライアントコードをリストに追加する方法を変更しました。clientCodesList = context.Users.Select(u => u.ClientCode).ToList();しかし、それでもクライアントコードは重複して作成されます – Trev

+0

私は最終的にはif(clientCodeExists == false)に変更することで、 – Trev

関連する問題