gpt4 book ai didi

c# - 为什么 signinmanager.passwordsigninasync 方法在 MVC5 中一直返回失败

转载 作者:行者123 更新时间:2023-11-30 18:16:05 29 4
gpt4 key购买 nike

这是我的登录方法:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
//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(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, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}

这是我的授权过滤器:

protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var userEmailId = httpContext.Session["userName"];
bool authorize = false;
foreach (var role in allowedroles)
{
/* getting user form current context */
var user = context.employees.Where(m => m.emailId == userEmailId && m.Role == role);
if (user.Count() > 0)
{
authorize = true; /* return true if Entity has current user(active) with specific role */
}
}
return authorize;
}

这是我的操作方法:

[AuthAttribute]
[CustomAuthorize("Admin", "SuperAdmin")]
public ActionResult GetEmployeeList(string sortOrder, string currentFilter, string searchString, int? page)
{
return View(db.GetListviewData().ToList());
}

我尝试使用授权过滤器从数据库中检查用户角色,但是当我尝试登录我的应用程序时,SignInManager.PasswordSignInAsync 方法每次都返回失败。

请帮助我,在此先感谢

最佳答案

我有同样的问题,但我意识到它只发生在用户更改他们的电子邮件之后(显然 UserName 不再等于 Email)。

看一下 PasswordSignInAsync 的签名,第一个参数是 UserName,但 Visual Studio 项目模板中的默认代码具有你我都拥有的 Email。

改变:

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

收件人:

var result = await SignInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, shouldLockout: false);

关于c# - 为什么 signinmanager.passwordsigninasync 方法在 MVC5 中一直返回失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47057771/

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