asp.net-mvc - ASP.NET MVC 登录成功后未登录

标签 asp.net-mvc cookies 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);

=> 登录端点

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

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

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

我尝试了以下方法:

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

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

有人能找到解决方案或解决方法吗?

最佳答案

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

我是通过开发者工具(Application > Cookies)做到的,但这似乎还不够。

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

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

虽然我不能确定,但​​我假设这是一个浏览器问题,将其写为答案(并将标记为正确)。 如果我发现由于没有浏览器问题,同一网站再次出现这种情况,我将取消选中此作为正确答案。

关于asp.net-mvc - ASP.NET MVC 登录成功后未登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42925487/

相关文章:

c# - 从JSON返回的字符串中删除双引号

c# - 使用 OWIN 自托管 ASP.NET Web API 时,我应该如何存储每个请求数据

c# - 如何从 Owin 获取原始 url?

asp.net-mvc - 在 Web api 和类库中使用 unity DI

c# - Controller.File() 的有效内容类型是什么?

php - 在 codeception 中丢失 session /cookies - 我怎样才能保持登录到我的网站?

javascript - 关于登录 cookie 的建议

asp.net-mvc - Web API – 使用不记名 token 进行身份验证

c# - 在 ASP.NET MVC 中,没有 AntiForgeryToken 的删除操作方法不安全吗?

PHP:无法设置 Cookie