2017-01-05 3 views
0

サイトにログインする際に少し問題が発生しました。登録された電子メールアドレスがない場合、ページの検証が失敗し、電子メールアドレスフィールドの下に「電子メールアドレスが存在しない」などのメッセージが表示されます。 PasswordSignInAsyncが原因で例外がスローされますので、何のメールアドレスが見つからないとき登録されていないメールアドレスでログインすると例外が発生します

しかし、私は、ログインのために以下のコードは、そのの世話をすべきかわからない...

「ユーザー」以下、このコードではnullですuser.UserNameプロパティがnullです。

これの検証をどのように処理すればよいのですか?これは、ASP.Net MVCテンプレートのロジックに既に組み込まれていてはなりません。

[HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
    { 
     if (!ModelState.IsValid) 
     { 
      return View(model); 
     } 

     // Require the user to have a confirmed email before they can log on. 
     var user = await UserManager.FindByEmailAsync(model.Email); 
     if (user != null) 
     { 
      if (!await UserManager.IsEmailConfirmedAsync(user.Id)) 
      { 
       string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account-Resend"); 

       // Uncomment to debug locally 
       // ViewBag.Link = callbackUrl; 
       ViewBag.errorMessage = "You must have a confirmed email to log on. " 
            + "The confirmation token has been resent to your email account."; 
       return View("Error"); 
      } 
     } 

     // This doesn't count login failures towards account lockout 
     // To enable password failures to trigger account lockout, change to shouldLockout: true 
     var result = await SignInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, shouldLockout: false); 
     switch (result) 
     { 
      case SignInStatus.Success: 
       return RedirectToLocal(returnUrl); 
      case SignInStatus.LockedOut: 
       return View("Lockout"); 
      case SignInStatus.RequiresVerification: 
       return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); 
      case SignInStatus.Failure: 
      default: 
       ModelState.AddModelError("", "Invalid login attempt."); 
       return View(model); 
     } 
    } 

答えて

1

メッセージ「無効なログインの試行は、」電子メールアドレスフィールドの検証エラーと表示されていることをしたい場合は、E-を保持しているあなたのViewModelの特定のプロパティにModelErrorを設定する必要がありますメールアドレス。

「EmailAdressは」ViewModelにプロパティの名前であるのに対し、だから、それは、次のようになります。

ModelState.AddModelError("EmailAddress", "Invalid login attempt."); 
関連する問題