GetExternalLoginInfoAsync()loginInfo返回null - 但仅在几个小时之后

时间:2016-04-06 18:09:00

标签: asp.net asp.net-mvc authentication owin azure-web-sites

我使用Strava作为我的外部登录提供商(我认为这与Strava无关,也可能是google或facebook)运行几小时/几天甚至几周后,GetExternalLoginInfoAsync返回null。我已经阅读了同样问题的一堆其他问题,但没有找到解决方案。我发布了我的整个ConfigureAuth方法,以防我在订单上做错了。

如果你有一个strava帐户,你可能会在这里遇到问题:fartslek.no/Account/Login

    public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context, user manager and signin manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        // Configure the sign in cookie
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            },
            CookieManager = new SystemWebCookieManager()
        });            
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));

        app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);


        app.UseStravaAuthentication( new StravaAuthenticationOptions{
              ClientId="XXX",
              ClientSecret= "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",

        });
    }

我使用此https://github.com/Johnny2Shoes/Owin.Security.Strava来获取StravaAuth。

当它停止工作时,天蓝色的重置是不够的,但是如果我进行新的部署,一切都会工作一段时间。

我使用的是Owin 3.0.1和Mvc 5.2.3

1 个答案:

答案 0 :(得分:9)

我遇到了同样的问题。谷歌搜索了一下之后,我发现这是Owin的一个已知错误,因为它们处理cookie的方式。

This issue已提交给Katana团队,但看起来他们根本没有解决这个问题。有很多解决方法,但这是我能找到的最简单的方法:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult ExternalLogin(string provider, string returnUrl)
    {
        ControllerContext.HttpContext.Session.RemoveAll();

        // Request a redirect to the external login provider
        return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
    }

有关此错误的更多详细信息,请参阅this问题,如果适合您,请与我们联系。