gpt4 book ai didi

asp.net-mvc - UserManager.FindByEmail() 返回 Null

转载 作者:行者123 更新时间:2023-12-05 07:55:11 24 4
gpt4 key购买 nike

我正在尝试在 AspNet.Identity for MVC5 中实现基于电子邮件地址的用户名。只要系统上有注册的电子邮件/用户名,我的应用程序就可以找到。

我刚刚发现,如果用户不存在并尝试登录,则会在第 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);
}
}

谢谢!

最佳答案

我尝试了下面的代码并为我工作:

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

希望它有用,即使过了那么久。

请让我们知道您的问题是否已解决。问候。

关于asp.net-mvc - UserManager.FindByEmail() 返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30306510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com