我試圖實現基於電子郵件地址的用戶名在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);
}
}
謝謝!
同樣的問題。實際上所有Find *方法都由於某種原因返回null。目標用戶在創建應用時被播種。 – Ryan
已解決。剛剛錯了Auth中間件配置。 – Ryan