我已经将一个KubernetesCore2.0Web应用程序部署到了ASP.NET集群。应用程序正在使用Azure AD对某些受保护的页面进行身份验证。Kubernetes集群设置了Nginx入口控制器,让我们加密以支持https。
我可以毫无问题地访问https://x.eastus.cloudapp.azure.com,通过点击网站上的链接,我被定向到https://x.eastus.cloudapp.azure.com/link,也没有任何问题。
但是,当我单击一个需要登录用户的链接时,我会得到:
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文件:
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中有以下代码:
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();
}发布于 2018-11-13 20:16:38
在Configure方法中添加一个自定义中间件,以执行手动http-https重定向
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);
}
});发布于 2020-08-19 22:31:46
我知道这是旧的,但我在ASPWebCore3和带有自定义OpenID连接提供程序的IdentityModel库中遇到了类似的问题。重定向urls也将是http而不是https。
为了最终让它正常工作,我必须做很多事情:
以下是相关的启动代码(请务必根据您的集群修改网段:
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。
https://stackoverflow.com/questions/49820108
复制相似问题