【问题标题】:Azure OpenID Connect via OWIN Middleware resulting in Infinite Redirect LoopAzure OpenID Connect 通过 OWIN 中间件导致无限重定向循环
【发布时间】:2015-02-16 00:00:30
【问题描述】:

我已经使用 OWIN 中间件在我的 ASP.NET MVC 应用程序中设置了 OpenID Connect 身份验证。

正如此 Fiddler 输出所示,一旦通过 Azure OpenID Connect 成功登录,浏览器就会不断地在我的 site.azurewebsites.net 和 login.windows.net 之间来回循环。

我已确保以下键与 Azure AD 信息正确匹配

<add key="ida:AADInstance" value="https://login.windows.net/{0}" />
<add key="ida:Tenant" value="******.onmicrosoft.com" />
<add key="ida:ClientId" value="*******" />
<add key="ida:PostLogoutRedirectUri" value="*********" />

而我的Start.cs代码如下

 private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
    private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
    private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
    private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];

    private string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);

    IAuthorizationService authorizationService = new AuthorizationService();

    public void ConfigureAuth(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions()
        {

            ExpireTimeSpan =TimeSpan.FromMinutes(15)
        });

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = authority,
                PostLogoutRedirectUri = postLogoutRedirectUri}
            });
    }
}

不确定是什么导致它不断重定向。我在 MVC 控制器上放置了一个 [Authorize] 属性,该控制器是 Post Authentication Redirect Url 所在的位置。

【问题讨论】:

  • 您最初是否仅通过 http 访问该网站?不是https?我有同样的情况,如果我只用 http 访问网站,我会被重定向到登录,获得身份验证,重定向回来并最终进入一个循环。最终强制使用 https。
  • 一切都通过 HTTPS 访问。
  • 您找到解决问题的方法了吗?我有一个类似的问题。下面的答案没有帮助。
  • 没有找到解决方案,因此最终放弃了 Open ID Connect。我现在使用 ASP.NET Identity 对 Azure AD 进行身份验证。看看这个rickrainey.com/2014/08/19/…

标签: asp.net-mvc azure oauth-2.0 openid owin


【解决方案1】:

我昨晚在一个 ASP.NET Framework 4.5.1 MVC 应用程序中遇到了这个问题。对我来说有两个问题。

  1. 尝试使用 HTTP 而不是 HTTPS 访问网站

    • 我有 localhost、dev 和 test(当然还有 prod)环境在 HTTPS 下运行,所以我不必担心在本地处理 HTTP。我到处都有 HSTS 运行,这解决了这个问题。有关更多信息,请参阅 Best way in asp.net to force https for an entire site?
  2. 此处描述的 Cookie 覆盖 https://github.com/aspnet/AspNetKatana/wiki/System.Web-response-cookie-integration-issues

    • 对我有用的是重新配置 CookieAuthenticationMiddleware 以直接写入 System.Web 的 cookie 集合 修复结合 Katana 3.1.0 有多个可用的 ICookieManager 实现。旧版本可以使用以下修复

在我找到解决方法之前,我是一个“我尝试了所有方法但没有任何效果”的开发人员。希望这也适用于您。

【讨论】:

  • 我根据提供的链接更新了我的 URL 重写。
  • 对我来说,重定向循环似乎是由 OWIN 和 System.Web 之间的冲突引起的,如下所述:System.Web response cookie integration issues。我将CookieManager = new SystemWebCookieManager() 添加到UseCookieAuthentication 和`UseOpenIdConnectAuthentication` 中,似乎已经解决了这个问题。
【解决方案2】:

这里发生的事情与 JuneT 注意到的有关。这与 CookieAuthenticationOptions.CookieSecure == CookieSecureOption.SameAsRequest 的默认值有关。由于您从 http 开始,因此最终重定向到 http。创建“authcookie”的请求是来自 AAD 的 https。

我可以通过设置 CookieSecure == CookieSecureOption.Always 来完成这项工作。这意味着 cookie 可能会与您的身份验证一起泄漏。

是否必须有一种方法来确保仅进行身份验证的页面将接受 https 上的连接。

【讨论】:

  • 我通过这个强制我的(注意,在我的情况下,我希望整个站点都在 https 中):
【解决方案3】:

要解决此问题:您可以升级您的应用程序以使用 ASP.NET Core。如果您必须继续使用 ASP.NET,请执行以下操作: 将应用程序的 Microsoft.Owin.Host.SystemWeb 包更新为至少版本。 修改您的代码以使用新的 cookie 管理器类之一,例如:

app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
    AuthenticationType = "Cookies", 
    CookieManager = new Microsoft.Owin.Host.SystemWeb.SystemWebChunkingCookieManager() 
});

Reference Link

【讨论】:

    【解决方案4】:

    通过确保请求在重定向到 Azure 之前使用 https 来解决此问题

                app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
                {
                    ClientId = AppConfig.ClientId,
                    Authority = AppConfig.Authority,
    
                    Notifications = new OpenIdConnectAuthenticationNotifications
                    {
                        RedirectToIdentityProvider = context =>
                           {
                               if (context.ProtocolMessage.RequestType == OpenIdConnectRequestType.AuthenticationRequest)
                               {
                                   // ensure https before redirecting to Azure
                                   if (!context.Request.IsSecure)
                                   {
                                       context.Response.Redirect(string.Format("https://{0}{1}", context.Request.Uri.Authority, context.Request.Uri.AbsolutePath));
                                       context.HandleResponse();
                                       return Task.FromResult(0);
                                   }
                               }
    
                               return Task.FromResult(0);
                           },
    
                        AuthenticationFailed = context =>
                                        {
                                            context.HandleResponse();
                                            context.Response.Redirect(AppConfig.RedirectUri + "SignInError?message=" + context.Exception.Message);
                                            return Task.FromResult(0);
                                        },
                    },
                });
    

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题,并通过使用 nuget 包 kentor.owincookiesaver 修复了它。使用代码如下:-

      public void ConfigureAuth(IAppBuilder app)
      {
      app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
      
      app.UseKentorOwinCookieSaver();//Workaround for infinite loop between webapp & login page
      
      app.UseCookieAuthentication(new CookieAuthenticationOptions());
      app.UseOpenIdConnectAuthentication(CreateOptionsFromPolicy(SignUpPolicyId));
      app.UseOpenIdConnectAuthentication(CreateOptionsFromPolicy(ProfilePolicyId));
      app.UseOpenIdConnectAuthentication(CreateOptionsFromPolicy(SignInPolicyId));
      }
      

      【讨论】:

        猜你喜欢
        • 2018-10-16
        • 2016-05-15
        • 1970-01-01
        • 2011-06-04
        • 1970-01-01
        • 2019-04-01
        • 1970-01-01
        • 2016-03-14
        • 2023-03-12
        相关资源
        最近更新 更多