【问题标题】:Redirect URI sent as HTTP and not HTTPS in app running HTTPS在运行 HTTPS 的应用程序中以 HTTP 而非 HTTPS 形式发送的重定向 URI
【发布时间】:2018-11-01 05:49:36
【问题描述】:

我有一个 Asp .net 核心 MVC 应用程序。它连接到 Identity Server 4 进行身份验证。托管在 docker swarm 中

MVC 应用托管在https://XXXXXXX

配置服务

services.AddAuthentication(options =>
        {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
        })
             .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
            {
                //options.DataProtectionProvider = DataProtectionProvider.Create(new DirectoryInfo(@"C:\temp-keys\"));
                // when the identity has been created from the data we receive,
                // persist it with this authentication scheme, hence in a cookie
                options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                // Identity server endpoint
                options.Authority = settingsSetup.IdentityServerEndpoint;

                // Client id to login with
                options.ClientId = settingsSetup.ClientId;
                // Client secret.
                options.ClientSecret = settingsSetup.Secret;

                // Scope of our API
                options.Scope.Add("testapi");
                options.Scope.Add("devconsole");
                // adding offline_access to get a refresh token
                options.Scope.Add("offline_access");

                options.ResponseType = "code id_token";
                options.SaveTokens = true;
                options.GetClaimsFromUserInfoEndpoint = true;
            });

当我尝试运行应用程序时,我收到重定向 uri 未匹配错误。

Invalid redirect_uri: http://developerconsole.XXXXX.io/signin-oidc
{
  "ClientId": "BB1D2DA8-D7E4-4AF5-94FA-19EAD6B7D711.apps.XXXXX.biz",
  "ClientName": "Developer Console",
  "AllowedRedirectUris": [
    "http://localhost:55000/signin-oidc",
    "http://localhost:55000/auth.html",
    "http://localhost:55000/auth-silent.html"
    "https://developerconsole.XXXXX.io/signin-oidc"
  ],
  "SubjectId": "21379983",
  "RequestedScopes": "",
  "Raw": {
    "client_id": "BB1D2DA8-D7E4-4AF5-94FA-19EAD6B7D711.apps.XXXXX.biz",
    "redirect_uri": "http://developerconsole.XXXXX.io/signin-oidc",
    "response_type": "code id_token",
    "scope": "openid profile testapi devconsole offline_access",
    "response_mode": "form_post",
    "nonce": "636625889658410682.MjNlMmQwNjgtZmY0MC00MmVkLWFiNmMtN2M2YmQ5YTM5ZTQ3NjFiYzI2ZjktZWM0Yi00NDk3LTk1ZWMtNjJkYjViMDYwMTJm",
    "state": "CfDJ8Pwa8A3ipXlKtuyxNMpMxAz5QUFmdSunRKdlKS9sS390AKp8gIUZShQUMMCkFAhYLytitgsXUBgwlQDJaJvtHFqzHygLCPwS8Jab6IJzhpry90qS51E1y_eRlppamRDOzYDZ6fcDFzWV1U43BTP2B6pnPTSLNcZRaooyGBXtNokeUqOJ--u-_MOQB8Bw3n2cRyV4kisHNkslD1Gsi2wn1Cx6aTVlqzw_pxHelAXm1P8FyDJpD7G0azFgKgpQF0DRJtC5penRJQzHIHvQN8v4ECGeuSD1zlyfJYClLO2r6kY_R2OYqtBkV0r_SNc9h7xUYmnVaHKQzYqVc_mJO4iLLSMTZrBUICZWR8c4PZw0Os3N",
    "x-client-SKU": "ID_NET",
    "x-client-ver": "2.1.4.0"
  }
}

错误来了,因为我有 "https://developerconsole.XXXXX.io/signin-oidc" 作为重定向 uri 而不是 "http://developerconsole.XXXXX.io/signin-oidc" 我不想添加 HTTP 重定向 uri。

为什么我的应用构建的重定向 uri 有 http 而不是 https?

如果我确实添加了 HTTP,我会收到一个烦人的关联错误。我认为这是因为它被服务器作为 https 返回,因为服务器会自动将 http 转换为 https。

处理请求时发生未处理的异常。 例外:关联失败。 Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler+d__12.MoveNext()

堆栈查询 Cookie 标头异常:关联失败。 Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler+d__12.MoveNext() System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Authentication.AuthenticationMiddleware+d__6.MoveNext() System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+d__7.MoveNext()

我可能不需要提及这在 localhost 上可以正常工作:/

【问题讨论】:

  • 这可能是因为您在 (L7) 负载平衡器后面,而平衡器后面的内部通信发生在 HTTP 中,而不是 HTTPS?
  • 好吧,我知道我们有一个负载均衡器,而且我知道该找谁出问题。
  • ...在这种情况下,Microsoft.AspNetCore.HttpOverrides 中的转发标头中间件可能会有所帮助。看这里:docs.microsoft.com/en-us/aspnet/core/host-and-deploy/…
  • 仅在必要时与负载平衡器管理员联系以确保 X-Forwarded-ForX-Forwarded-ProtoX-Forwarded-Host 标头包含在转发的请求中。可能是。
  • 刚刚有人告诉我 (l4) 码头工人群中的那个。这适用于另一个项目@spender 想知道他是否已将其添加到该项目而不是这个项目。

标签: c# oauth asp.net-core-mvc identityserver4


【解决方案1】:

解决方案非常简单。通过设置 UseForwardedHeaders,它现在将所有请求作为 HTTPS 发送。

app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedProto
        });

关联失败。

现在已修复,我不再需要 http 和 https 重定向 uri。

【讨论】:

  • 如果应用程序部署在 Linux 上(比如 .NET 核心应用程序在 linux docker 容器上),除了设置 Forward 标头外,还必须清除 KnownNetworksKnownProxies确保重定向 url 仍然是 https。有关详细信息,请参阅docs.microsoft.com/en-us/aspnet/core/host-and-deploy/…
  • @SuketuBhuta 那一点信息真的很有用
  • 我们不得不使用这个解决方案,因为我们在 aws ELB 后面的 NGINX conf 中将 HTTP 重定向到 HTTPS。它对我们来说就像魅力一样。
  • @SuketuBhuta 你是如何/在哪里/何时清除KnownNetworksKnownProxies 的?
  • @David,请参阅我上面链接的文章的这一部分:docs.microsoft.com/en-us/aspnet/core/host-and-deploy/…,基本上你在设置ForwardingHeader 后就清楚了。希望这会有所帮助。
【解决方案2】:

Ubuntu 18、Nginx、.NetCore 3.1 它的工作方式如下:

app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.All
            });
 

/etc/nginx/nginx.conf

fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;

/etc/nginx/sites-enabled/default 或您的网络配置文件

location / {

# under your configuration
    proxy_set_header X-Forwarded-Proto $scheme;
}

然后它开始重定向到 https,没有 nginx 配置我得到错误 503。

【讨论】:

  • 你用 .net 6 测试过吗?
  • 不,我还没有更新项目。当我愿意的时候,我也会编辑这个回复
【解决方案3】:

只需将此代码添加到您的 web.config 文件,当然还有 change.com 与您的网站,任何人在浏览器中输入 URL 直接将 URI 从 HTTP 重定向到 HTTPS。

<system.webServer>
    <rewrite>
      <rules>
        <rule name="IP Hit" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="http://www.exemple.com" />
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://www.exemple.com/{R:1}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
    <urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true" />
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365:00:00" />
    </staticContent>
 <system.webServer>

【讨论】:

  • 这个答案只会将目的地从 http 移动到 https,它不会使链接在 https 中呈现。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-06
  • 2017-05-15
相关资源
最近更新 更多