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

标签 asp.net-mvc oauth-2.0 google-oauth asp.net-mvc-5 facebook-oauth

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

有时,我可以使用Facebook或Google进行很好的身份验证。效果很好。

但是,经常发生的是:

  • 导航到登录页面。
  • 选择google或facebook。
  • 提供帐户信息,以获取必要的重定向。
  • 重定向回到登录页面,但未登录。

  • 我没有收到(或找不到合适的位置)任何提示我的错误-我在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();
    }
    

    我已按照本教程在MVC5中使用OAuth:Create an ASP.NET MVC 5 App with Facebook, Twitter, LinkedIn and Google OAuth2 Sign-on (C#)

    最佳答案

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

    app.UseKentorOwinCookieSaver();
    
    app.UseCookieAuthentication(new CookieAuthenticationOptions());
    

    显然,owin和katana中存在一个错误,您的cookie刚刚消失了,此问题得以解决。

    关于asp.net-mvc - 在MVC5中使用OAuth时,无限循环返回到身份验证页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21168686/

    相关文章:

    c# - HTTP 错误 414。请求 URL 太长。网站

    django - Apache 剥离 "Authorization" header

    node.js - 如何修复 node-linkedin 中的 CSRF 问题?

    Golang token 验证错误

    google-cloud-platform - 从运行在 Google Cloud VM 实例上的应用程序访问 Google Secrets - 将 Cloud API 分配给 VM

    java - 获取通过 OAuth2 验证的用户电子邮件地址

    apache - Apache 反向代理中的 Google OAuth 重定向 Uri

    asp.net-mvc - 我应该实现 OpenID 身份验证吗?美国/日本用户

    c# - 在 WebApi 或 MVC Controller 中使用 ConfigureAwait(false) 有什么危险吗?

    asp.net-mvc - “Security aware” Action 链接?