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

时间:2017-03-21 11:14:24

标签: 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);

=&GT;登录端点

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

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

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

我尝试了以下内容:

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

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

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

1 个答案:

答案 0 :(得分:1)

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

我是通过开发者工具(应用程序&gt; Cookie)完成的,但似乎这还不够。

通过设置(隐私&gt;内容设置&gt;所有Cookie和网站数据)再次删除Cookie后,登录正常。

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

虽然我无法确定这一点,但我认为这是一个答案(并且会标记为正确),假设这是一个浏览器问题。 如果由于没有浏览器问题我发现这种情况再次出现在同一个网站上,我将取消选中此作为正确的答案。