首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >回复地址中是否缺少ASP.NET核心2.0、Kubernetes、https?

回复地址中是否缺少ASP.NET核心2.0、Kubernetes、https?
EN

Stack Overflow用户
提问于 2018-04-13 23:08:22
回答 2查看 986关注 0票数 2

我已经将一个KubernetesCore2.0Web应用程序部署到了ASP.NET集群。应用程序正在使用Azure AD对某些受保护的页面进行身份验证。Kubernetes集群设置了Nginx入口控制器,让我们加密以支持https。

我可以毫无问题地访问https://x.eastus.cloudapp.azure.com,通过点击网站上的链接,我被定向到https://x.eastus.cloudapp.azure.com/link,也没有任何问题。

但是,当我单击一个需要登录用户的链接时,我会得到:

代码语言:javascript
复制
Sign in
Sorry, but we’re having trouble signing you in.

AADSTS50011: The reply address 'http://x.eastus.cloudapp.azure.com/signin-oidc' does not match the reply addresses configured for the application: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx'. More details: not specified

请注意,上面的URL缺少https,这就是问题所在。

我已经在Azure AD中注册了"https://x.eastus.cloudapp.azure.com/signin-oidc“作为应用程序的回复URL。

但是,我不明白为什么在登录时使用的回复url缺少https。

如果我将完全相同的应用程序部署到Azure Web应用程序,我就不会遇到这个问题。

可能的问题是什么?

这是我的Ingress文件:

代码语言:javascript
复制
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: x-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    # Add to generate certificates for this ingress
    kubernetes.io/tls-acme: 'true'
spec:
  rules:
    - host: x.eastus.cloudapp.azure.com
      http:
        paths:
          - path: /
            backend:
              serviceName: x-service
              servicePort: 80
  tls:
    # With this configuration kube-lego will generate a secret called `x-tls-secret`
    # for the URL `x.eastus.cloudapp.azure.com`
    - hosts:
        - "x.eastus.cloudapp.azure.com"
      secretName: x-tls-secret

我在Startup.cs中有以下代码:

代码语言:javascript
复制
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<ForwardedHeadersOptions>(options =>
    {
        options.ForwardedHeaders =
            ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
    });

    services.Configure<MvcOptions>(options =>
    {
        options.Filters.Add(new RequireHttpsAttribute());
    });

    services.AddAuthentication(sharedOptions =>
    {
        sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    })
    .AddAzureAd(options => Configuration.Bind("AzureAd", options))
    .AddCookie();

    services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseForwardedHeaders();

    app.UseStaticFiles();

    app.UseAuthentication();
}
EN

回答 2

Stack Overflow用户

发布于 2018-11-13 20:16:38

在Configure方法中添加一个自定义中间件,以执行手动http-https重定向

代码语言:javascript
复制
app.Use(async (context, next) =>
{
    if (context.Request.IsHttps || context.Request.Headers["X-Forwarded-Proto"] == Uri.UriSchemeHttps)
    {
        await next();
    }
    else
    {
        string queryString = context.Request.QueryString.HasValue ? context.Request.QueryString.Value : string.Empty;
        var https = "https://" + context.Request.Host + context.Request.Path + queryString;
        context.Response.Redirect(https);
    }
});
票数 0
EN

Stack Overflow用户

发布于 2020-08-19 22:31:46

我知道这是旧的,但我在ASPWebCore3和带有自定义OpenID连接提供程序的IdentityModel库中遇到了类似的问题。重定向urls也将是http而不是https。

为了最终让它正常工作,我必须做很多事情:

  • 配置转发报头,以告知ASP核心使用入口正在发送的x-forwared-for和x-forwarded-proto报头
  • 告诉ASP网络.NET核心信任来自Kubernetes网络的代理
  • 提高转发限制,告知ASP .NET核心可以发生多少转发

以下是相关的启动代码(请务必根据您的集群修改网段:

代码语言:javascript
复制
  public void ConfigureServices(IServiceCollection services)
  {
    services.Configure<ForwardedHeadersOptions>(options =>
    {
      // This tells the middleware to update IP and scheme according to forwarded headers
      options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
      options.ForwardLimit = 2;

      // Add Kubernetes Networks to trusted networks
      options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse("10.42.0.0"), 16));
      options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse("the IP range of our cluster nodes"), 27));
    });

    // ...
  }

  public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
  {
    if (env.IsDevelopment())
    {
      app.UseDeveloperExceptionPage();
    }
    else
    {
      app.UseExceptionHandler("/Error");

      // Enable to process forward headers from https proxy like ingress nginx, this middleware must run before others
      // configuration is above in ConfigureServices
      app.UseForwardedHeaders();
    }

    // ...
  }

奇怪的是,这只会在某些时候起作用。有时,它仍然会尝试重定向到HTTP页面,在重新部署之后,它会突然工作。因此,我还在Configure方法中添加了app.UseHttpsRedirection();,它似乎每次都能工作。

编辑:在this blog中稍低一点,它解释了为什么需要将ForwardLimit至少设置为2。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49820108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档