2015-05-18 62 views
1

我試圖實現基於電子郵件地址的用戶名在AspNet.Identity爲MVC5。只要系統上有註冊的電子郵件/用戶名,我的應用程序就可以工作。UserManager.FindByEmail()返回null

我剛剛發現,如果用戶不存在,並試圖登錄異常的72號線

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:
Line 71: //Add this to check if the email was confirmed.
Line 72: var userid = UserManager.FindByEmail(model.Email).Id;

得到投擲這裏是我的代碼。

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
{ 
    if (ModelState.IsValid) 
    { 
     //Add this to check if the email was confirmed. 
     var userid = UserManager.FindByEmail(model.Email).Id; 
     // **Line 72. 'userid' is empty.** 

     // Here is my attempt but doesn't do anything. 
     if (string.IsNullOrEmpty(userid)) { 
      ModelState.AddModelError("","You are not registered!"); 
     } 

     if (!UserManager.IsEmailConfirmed(userid)) 
     { 
      ModelState.AddModelError("", "E-mail address has not been confirmed."); 
      return View(model); 
     } 
    } 
    var result = await SignInManager.PasswordSignInAsync(model.Email, 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 }); 
     case SignInStatus.Failure: 
     default: 
      ModelState.AddModelError("", "Invalid login attempt."); 
      return View(model); 
    } 
} 

謝謝!

+0

同樣的問題。實際上所有Find *方法都由於某種原因返回null。目標用戶在創建應用時被播種。 – Ryan

+0

已解決。剛剛錯了Auth中間件配置。 – Ryan

回答

0

我添加了下面的代碼,它的工作原理,但我仍然不明白如何FindByNameAsyn()方法實際工作,而不是FindByName()?還有 有沒有更好的方法來做到這一點?謝謝!

//代碼工作。

var user = await UserManager.FindByNameAsync(model.Email); 
       if (user == null) 
       { 
        ModelState.AddModelError("", "Invalid login attempt."); 
        return View(model); 
       } 
0

我嘗試下面的代碼,併爲我工作:

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

     string userName = ""; // to be used as arg to PasswordSignInAsync 

     // you'd better declare the appropriate Type. 
     // using "var" doesn't work here - I don't know why... 
     ApplicationUser user = await UserManager.FindByEmailAsync(model.UserName); 

     if (user != null) 
     { 
      // found an existing user having the given Email 
      // so let's get it's UserName to test SignIn 
      userName = user.UserName; 
      // just for debugging - check your AspNetUser table 
      // ModelState.AddModelError("", userName + " ID = " + user.Id.ToString()); 
     } 
     else 
     { 
      // Hum... no email found - perhaps the user is really trying the UserName 
      // Perhaps Email != UserName 
      // It's interesting to give the user the option to use Email or UserName 
      // to get authenticated. 
      // Let's play, then! 
      userName = model.UserName; 
     } 

     // 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(userName, model.Password, 
           model.RememberMe, shouldLockout: true); 

     // from here on, it's the scaffolded code... 

     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); 
     } 
    } 

希望這是有用的,甚至所有的時間之後。

請讓我們知道您的問題是否已解決。 此致敬禮。