joa*_*ofe 11 oauth-2.0 azure-web-sites azure-active-directory asp.net-core
我面临以下问题.我有一个ASP Net Core 2 Web应用程序,我想部署到Azure.应用程序身份验证与Azure Active Directory集成,因此当我尝试登录时,会发生以下请求:
GET https://login.microsoftonline.com/ecf3f643-27e5-4aa7-9d56-fd350e1e9c37/oauth2/authorize?client_id=20a2bcb5-0433-4bb4-bba3-d7dc4c533e85&redirect_uri=http://myapplication.mydomain.com/account/signin [...] 200 OK
POST http://myapplication.mydomain.com/account/signin 301 Redirect --> https://myapplication.mydomain.com/account/signin
GET https://myapplication.mydomain.com/account/signin 500 Internal Server Error
Run Code Online (Sandbox Code Playgroud)
第一个GET是正常的Azure Active Directory登录请求.注意redirect_uri参数有协议http.
第二个请求是重定向到redirect_uri带有一些参数的POST.由于我已将Azure配置为仅允许HTTPS流量,因此IIS使用HTTPS重定向到相同的URL.这是第三个要求.请注意,第三个请求是GET请求,因为HTTP重定向始终是GET请求,POST请求的所有参数都会丢失,并且身份验证失败会在后端发出HTTP 500错误.
我试图手动将redirect_uri参数中的协议手动更改为HTTPS,并且它按预期工作.因此,我唯一需要的是让ASP Net Core意识到协议是HTTPS.
怎么办?我在互联网上搜索了大量的页面而没有明确的答案.
注意:redirect_uri由Kestrel设置.由于Azure App Service将IIS放在我的Kestrel前面并在那里进行SSL终止,因此Kestrel和我的应用程序不知道协议是HTTPS,因此在重定向uri中使用HTTP.
更新1
按照@Bruce的建议,我在这里尝试了这个例子,克隆了存储库,并按照那里的说明配置了应用程序和AD,我能够重现错误.
重定向URI继续与http协议一起使用.如果我只在AD应用程序配置中添加https端点作为回复URL,则会收到错误The reply address 'http://testloginad.azurewebsites.net/signin-oidc' does not match the reply addresses configured for the application.如果我将http协议端点添加为回复URL,则会收到如下所示的HTTP 500错误:
System.Exception: Correlation failed.
at Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1.<HandleRequestAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.<Invoke>d__6.MoveNext()
Run Code Online (Sandbox Code Playgroud)
我仍然认为问题与Kestrel有关,不知道通过HTTPS完成连接,但我不知道如何将信息传达给它.
更新2
我使用的Azure Web应用程序的配置:
在web.config文件中我修改了以下行,如下所示:
<aspNetCore processPath="dotnet" arguments="./WebApp-OpenIDConnect-DotNet.dll" stdoutLogEnabled="false" stdoutLogFile="./stdout.log" />
Run Code Online (Sandbox Code Playgroud)
基本上我提出斜杠而不是反斜杠,以避免Linux路径出现问题.
使用默认设置配置其他所有内容.
更新3 根据@Tratcher的要求,我在这里添加服务器响应的标题(为了简洁起见,我只包括我认为相关的标题,如果你想看到任何其他标题,请随时让我添加它) :
GET https://login.microsoftonline.com/ecf...):
Microsoft-IIS/10.0ESTSAUTHPERSISTENT=AQAFCCEADDB…sts; path=/; secure; HttpOnlymax-age=31536000; includeSubDomainsPOST http://testloginad.azurewebsites.net/signin-oidc):
https://testloginad.azurewebsites.net/signin-oidcMicrosoft-IIS/10.0GET https://testloginad.azurewebsites.net/signin-oidc):
Kestrelx-forwarded-proto任何请求中都不会出现标头.
请注意,问题的根源可能在第二个请求的重定向中,即将HTTP POST重定向到HTTPS GET.该重定向不应该发生,因为首先应该通过HTTPS请求POST,但是由于第一个请求的redirect_uri中的http协议错误而没有发生.
更新4
我已经确认只有选择的服务计划是Linux服务计划才会出现此问题.如果服务计划是Windows服务计划(使用与UPDATE 1示例完全相同的代码和配置),则根本不会发生此问题.对于问题,这可能是一种解决方法,但不是解决方案.Linux应用程序服务似乎有缺陷.
我自己有问题。我深入研究了Microsoft的Microsoft.AspNetCore.Authentication,并了解了他们如何构造重定向URL:
protected string BuildRedirectUri(string targetPath)
=> Request.Scheme + "://" + Request.Host + OriginalPathBase + targetPath;
Run Code Online (Sandbox Code Playgroud)
由于Web App已经强制执行HTTPS,因此可以使用Startup.cs中的以下代码解决
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedProto
});
Run Code Online (Sandbox Code Playgroud)
您只需要添加以下参考:
using Microsoft.AspNetCore.HttpOverrides;
Run Code Online (Sandbox Code Playgroud)
通过查阅这些链接:
对配置进行 3 处更改后,我的 Linux 应用程序计划一切正常。
步骤 1:配置ForwardedHeadersOptions
services.Configure<ForwardedHeadersOptions>(options =>
{
options.RequireHeaderSymmetry = false;
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
// TODO : it's a bit unsafe to allow all Networks and Proxies...
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
Run Code Online (Sandbox Code Playgroud)
步骤 2:方法中的UseForwardedHeaderspublic void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseForwardedHeaders();
Run Code Online (Sandbox Code Playgroud)
第 3 步:仅在生产中使用UseHttpsRedirection
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
// Forward http to https (only needed for local development because the Azure Linux App Service already enforces https)
app.UseHttpsRedirection();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
Run Code Online (Sandbox Code Playgroud)
我面临以下问题。我有一个 ASP Net Core 2 Web 应用程序,我想将其部署到 Azure。应用程序身份验证与 Azure Active Directory 集成。
因为您没有提到如何将 AAD 身份验证集成到您的 Web 应用程序中。此外,我已经检查过,当通过http://analytics.lantek360.com或访问您的应用程序时https://analytics.lantek360.com,redirect_uri查询字符串将相同:http://analytics.lantek360.com/account/signin。您可以提供更多详细信息(例如您如何构建授权请求),以便我们缩小此问题的范围。
由于我已将 Azure 配置为仅允许 HTTPS 流量
仅 HTTPS 设置使用 URL 重写规则将 HTTP 重定向到 HTTPS。详细信息,您可以按照如何使Azure应用服务仅HTTPS。
根据您的要求,我假设您可以手动使用中间件Microsoft.AspNetCore.Authentication.OpenIdConnect将 Azure AD 集成到您的 .Net Core Web 应用程序中。对于这种方法,您可以按照以下教程进行操作:
将 Azure AD(v1.0 端点)集成到 ASP.NET Core Web 应用程序中
将 Azure AD(v2.0 端点)集成到 ASP.NET Core Web 应用程序中
笔记:
OpenID Connectredirect_uri的看起来像http(s)://<your-appname>.azurewebsites.net/signin-oidc。由于您只需要 Https,因此只需https://{your-appname}.azurewebsites.net/signin-oidc为 AAD 应用程序添加重定向 URI ( )。
此外,您还可以利用应用服务身份验证/授权来启用 AAD 身份验证,而无需更改 Web 应用程序中的代码。详细信息,您可以按照配置您的应用服务应用程序以在 Azure 门户中使用 Azure Active Directory 登录。
| 归档时间: |
|
| 查看次数: |
2383 次 |
| 最近记录: |