如果ASP .NET Identity Owin PasswordSignInAsync方法在某些情况下无法工作,可能是由于以下问题导致的:
以下是一些可能会导致ASP .NET Identity Owin PasswordSignInAsync方法无法工作的常见问题的代码示例:
错误的用户名/密码:
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); if (result == SignInStatus.Success) { return RedirectToLocal(returnUrl); } else if (result == SignInStatus.Failure) { ModelState.AddModelError("", "Invalid login attempt."); return View(model); }
在这种情况下,确保model.Email和model.Password包含正确的用户名和密码。
验证配置错误:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity
在这种情况下,确保AuthenticationType和LoginPath等选项正确设置。
会话丢失:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity