ASP.NET MVC 成功登录后未登录

Joa*_*iro 2 cookies asp.net-mvc owin

在我自己的机器和浏览器 (chrome) 中,我无法登录我的网站。它适用于其他浏览器、其他 chrome 用户和隐身窗口。它也适用于我的开发环境或同一网站的其他阶段。

我关于登录的相关代码如下:

=> StartUp.ConfigureAuth

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),
    Provider = new CookieAuthenticationProvider
    {
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, Data.DbContainer.Entities.User>(
            validateInterval: TimeSpan.FromMinutes(30),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
    }
});            
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
Run Code Online (Sandbox Code Playgroud)

=> 登录端点

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

    SignInStatus result;
    using (var mainDb = MainDbDataManager.GetInstance())
    {
        var user = await mainDb.UserManager.FindByNameAsync(model.Username);
        // Check if user has permission to access CMS
        if (user != null && !await mainDb.UserManager.IsInAnyRoleAsync(user.Id, Customer.RoleName, Administrator.RoleName))
        {
            result = SignInStatus.Failure;
        }
        else
        {
            using (var signInManager = HttpContext.GetOwinContext().Get<ApplicationSignInManager>())
            {
                result = await signInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, shouldLockout: false);
            }
        }

        switch (result)
        {
            case SignInStatus.Success:
                {
                    // Set user's language
                    Session[Core.Helpers.ConstantsHelper.SessionConstants.LanguageCodeKey] = user.Language.Code;

                    return RedirectToLocal(returnUrl);
                }
            case SignInStatus.LockedOut:
                ModelState.AddModelError("", Strings.LoginDisabledError);
                return View(model);
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", Strings.LoginFailedError);
                return View(model);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我调试了登录端点,发现登录成功,但 User.Identity.Username 为空,只要我的下一个端点中的 Request.IsAuthenticated 为 false。

我已经检查过this,但找不到成功的解决方案。

我尝试了以下方法:

  • 添加Session["Workaround"] = 0;Session_Start(),如上一个链接中所述。我不确定这是正确的地方,但似乎是这样。
  • 申请SystemWebCookieManager,如上一个链接中所述。
  • 删除浏览器的cookies
  • 重新启动浏览器
  • 调用AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);,即使没有登录的用户。

真不知道是浏览器问题还是开发问题。

任何人都可以找到解决方案或解决方法吗?

Joa*_*iro 6

正如我在问题中所说,我删除了浏览器(Chrome)的 cookie。

我是通过开发人员工具(应用程序 > Cookies)完成的,但似乎这还不够。

在我通过设置(隐私 > 内容设置 > 所有 cookie 和站点数据)再次删除 cookie 后,登录正常工作。

有关此操作的更多详细信息,请访问:https : //productforums.google.com/forum/#!topic/chrome/YEE24sDJxfo

尽管我无法确定,但我将其写为答案(并将标记为正确),假设它是浏览器问题。如果我发现由于没有浏览器问题而在同一个网站上再次发生这种情况,我将取消选中它作为正确答案。