在MVC5中使用OAuth时,无限循环返回到身份验证页面

stu*_*uck 8 asp.net-mvc oauth-2.0 facebook-oauth google-oauth asp.net-mvc-5

我写了一个网页,利用了使用MVC5和OAuth的Google/Facebook身份验证

有时候,我可以使用脸书或谷歌来表现出色.它运作得很好.

然而,经常会发生什么

  1. 导航到登录页面
  2. 选择谷歌或Facebook
  3. 提供帐户信息,获取必要的重定向
  4. 重定向回登录页面,但未登录

我没有收到(或者没有找到正确的地方)任何让我感到困惑的错误 - 我在Azure上使用SSL进行托管

有没有人提供有时为什么有效的提示,有时却没有?这感觉它可能是一个cookie的东西,或者可能是服务器端配置问题?我无法弄清楚为什么它有时会工作,有时候也无法工作.

我试过了

  • 使用第二台机器,一台从未登录过(以排除cookie),同样的问题
  • 清除我的cookie缓存,同样的问题

我是如何配置的:

        public void ConfigureAuth(IAppBuilder app)
        {
            // Enable the application to use a cookie to store information for the signed in user
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login")
            });
            // Use a cookie to temporarily store information about a user logging in with a third party login provider
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            // Uncomment the following lines to enable logging in with third party login providers
            //app.UseMicrosoftAccountAuthentication(
            //    clientId: "",
            //    clientSecret: "");

            //app.UseTwitterAuthentication(
            //   consumerKey: "",
            //   consumerSecret: "");

            app.UseFacebookAuthentication(
               appId: "abc",
               appSecret: "123");

            app.UseGoogleAuthentication();
        }
Run Code Online (Sandbox Code Playgroud)

我已按照本教程在MVC5中使用OAuth(http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-的oauth2-和OpenID的登录).J

Baa*_*ali 2

这是一个主要问题,您的应用程序将随机开始进入无限循环,有时重新部署应用程序可以使其工作,但只是暂时的。我发现解决这个问题的快速方法是使用kentor.owincookiesaver@cooper 评论的 nuget 包。您应该在 owin 启动类中调用 cookieauthentication 之前调用此类,如下所示

app.UseKentorOwinCookieSaver();

app.UseCookieAuthentication(new CookieAuthenticationOptions());
Run Code Online (Sandbox Code Playgroud)

显然,owin 和 katana 中有一个错误,你的 cookie 会消失,这可以修复它。