![](/img/trans.png)
[英]Microsoft .net core 2.0 Azure Data Lake and Azure Active Directory
[英].Net Core 2.0 - Azure Active Directory - NGinX reverse Proxy - HTTPS
我有一個.NET Core 2.0網站,在Linux上運行,偵聽端口5000,在NGinX反向代理后面,它正在偵聽端口80和442.NingXX配置為將HTTP流量重定向到HTTPS,並處理通過HTTPS的所有通信。 NGinx反向代理和Asp.Net核心應用程序位於共享網絡上的自己的docker容器中。
此設置目前正如所述工作。
但是,我現在想要針對Azure Active Directory驗證我的用戶。 我遇到了一個問題,不知道從哪里開始。
首先,讓我告訴你我如何配置大部分,然后我將解釋我的錯誤。
NGinx(nginx.conf)
worker_processes 2;
events { worker_connections 1024; }
http {
sendfile on;
upstream docker-dotnet {
server mycomp_prod:5000;
}
server {
listen 80;
server_name sales.mycompany.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name sales.mycompany.com;
ssl_certificate /etc/nginx/ssl/combined.crt;
ssl_certificate_key /etc/nginx/ssl/salesmycompany.key;
location / {
proxy_pass http://docker-dotnet;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
Dotnet:Startup.cs ConfigureServices()
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddAzureAd(options => Configuration.Bind("AzureAd", options))
.AddCookie();
Dotnet:Startup.cs Configure()
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseAuthentication();
Dotnet:appsettings.json
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "mycompany.com",
"TenantId": "my-tenant-id",
"ClientId": "my-client-id",
"CallbackPath": "/signin-oidc"
},
在My Azure Active Directory Tentant中
我已經配置了2個“回復網址”
現在是錯誤/我的問題
當我點擊該網站時,我會登錄。之后登錄,並選擇是否“保持登錄狀態”,我被帶到了一個有錯誤的頁面。
您可以在錯誤中看到,嘗試重定向到的回復地址是HTTP,而不是HTTPS。 我認為這是來自appsettings.json中的一行說:
"CallbackPath": "/signin-oidc"
由於我的.NET核心站點(通過端口5000上的Kestrel)不在HTTPS上,因此它嘗試加載http://example.com / signin-oidc而不是HTTPS。 請記住,我的.NET核心站點位於NGinX反向代理之后,該代理配置為處理443上的流量。
謝謝,
我看到了這個並且能夠使它工作: https : //github.com/aspnet/Security/issues/1702
var fordwardedHeaderOptions = new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
};
fordwardedHeaderOptions.KnownNetworks.Clear();
fordwardedHeaderOptions.KnownProxies.Clear();
app.UseForwardedHeaders(fordwardedHeaderOptions);
請確保您使用UseForwardedHeaders
之前中間件管道UseAuthentication
:
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseAuthentication();
Nginx將在將呼叫轉發給Kestrel之前為其分配一個標頭(以及其他內容)。 然后,此中間件將檢查這些標頭以設置正確的協議。
在startup.cs中,您可以嘗試強制使用主機名和架構。 以下代碼可以提供幫助。 重要: 在設置身份驗證之前放置它。
app.Use((context, next) =>
{
context.Request.Host = new HostString(hostname + (port.HasValue ? ":" + port : null));
context.Request.Scheme = protocol;
return next();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.