成功登录后未登录ASP.NET MVC [英] ASP.NET MVC Not logged in after successfully signed in

查看:175
本文介绍了成功登录后未登录ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我自己的机器和浏览器(Chrome浏览器)中,我无法登录我的网站.它可以在其他浏览器,Chrome的其他用户和隐身窗口中使用. 它也可以在我的开发环境或同一网站的其他阶段中使用.

In my own machine and browser (chrome), I am not able to login in my website. It works in other browsers, with other users of chrome and in incognito window. It also works in my development environment or in other stages of the same website.

我与登录有关的代码如下:

My relevant code regarding login is the following:

=> StartUp.ConfigureAuth

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

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

I debugged the login endpoint and I noticed that the sign in is successful, but the User.Identity.Username is null, as long as Request.IsAuthenticated in my next endpoint is false.

我已经,但我找不到成功的解决方案.

I checked already this, but I could not find a successful solution.

我尝试了以下操作:

  • 如前一个链接所述,将Session["Workaround"] = 0;添加到Session_Start().我不确定这是正确的地方,但似乎是正确的.
  • 应用SystemWebCookieManager,如上一链接所述.
  • 删除浏览器的cookie
  • 重新启动浏览器
  • 即使没有登录的用户,也会呼叫AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);.
  • Adding Session["Workaround"] = 0; to Session_Start(), as mentioned in the previous link. I am not sure this is the correct place, but it seems so.
  • Applying SystemWebCookieManager, as mentioned in the previous link.
  • Removing the cookies of the browser
  • Restarting the browser
  • Calling AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);, even though there is no logged user.

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

I really do not know whether this is a browser problem or a development problem.

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

Could anyone find a solution or a workaround?

推荐答案

我在问题中告诉我,我删除了浏览器(Chrome)的cookie.

As I told in the question, I removed the cookies of the browser (Chrome).

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

I did it through developer tools (Application > Cookies), but it seems this is not enough.

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

After I removed the cookies again through settings (Privacy > Content settings > All cookies and site data), the login worked properly.

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

More details about this action here: https://productforums.google.com/forum/#!topic/chrome/YEE24sDJxfo

尽管我不能确定,但​​我将其写为答案(并将标记为正确),前提是这是浏览器问题. 如果我发现由于没有浏览器问题而再次出现在同一网站上,我将取消选中它作为正确答案.

Although I am not able to be sure about it, I wrote this as an answer (and will mark as correct) supposing it was a browser problem. If I find out that this occurs again to the same website due to no browser issues, I will uncheck this as the correct answer.

这篇关于成功登录后未登录ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆