Ric*_*ley 9 c# azure-active-directory azure-ad-msal microsoft-graph-api microsoft-identity-web
我正在使用 .net 5 Identity Web Ui 访问 Microsoft Graph。在哪里可以配置我的重定向 URI?
我需要指定完整的 Uri,因为由于位于具有 SSL 卸载功能的负载均衡器后面,因此从 callbackUri 生成的 Uri 不正确。
这是我当前的配置服务部分
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"))
.EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
.AddMicrosoftGraph(Configuration.GetSection("DownstreamApi"))
.AddInMemoryTokenCaches();
Run Code Online (Sandbox Code Playgroud)
dav*_*-ao 11
我遇到了类似的问题,WebApp 仅暴露在前门后面,WebApp 必须调用自定义的下游 WebApi。
我的服务配置在我的本地主机开发机器上运行:
// AzureAdB2C
services
.AddMicrosoftIdentityWebAppAuthentication(
Configuration,
"AzureAdB2C", subscribeToOpenIdConnectMiddlewareDiagnosticsEvents: true)
.EnableTokenAcquisitionToCallDownstreamApi(p =>
{
p.RedirectUri = redUri; // NOT WORKING, WHY?
p.EnablePiiLogging = true;
},
[... an array with my needed scopes]
)
.AddInMemoryTokenCaches();
Run Code Online (Sandbox Code Playgroud)
我尝试了 AddDownstreamWebApi 但未能使其工作,因此我只是使用 ITokenAcquisition 获取所需的令牌并将其添加到 HttpClient 来发出我的请求。
然后我需要 AzureAd/B2C 登录重定向到带有前门 url 的 uri: https: //example.org/signin-oidc,事情就出问题了。我是这样解决的:
首先,您必须将此网址添加到天蓝色门户中的应用程序注册中,非常重要的是区分大小写,它关心尾随斜杠,我怀疑有许多指向同一控制器的网址,并且这些网址的顺序会产生一些影响,我只是删除了所有内容并保留了最低限度。
然后在配置服务方法中:
services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
options.SaveTokens = true; // this saves the token for the downstream api
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProvider = async ctxt =>
{
// Invoked before redirecting to the identity provider to authenticate. This can be used to set ProtocolMessage.State
// that will be persisted through the authentication process. The ProtocolMessage can also be used to add or customize
// parameters sent to the identity provider.
ctxt.ProtocolMessage.RedirectUri = "https://example.org/signin-oidc";
await Task.Yield();
}
};
});
Run Code Online (Sandbox Code Playgroud)
重定向成功了,但我在受保护页面和 AzureB2C 登录之间进入了循环。
成功登录并正确重定向到signin-oidc控制器(由Identity.Web包创建)后,我再次正确地重定向到启动所有这些授权事情的页面,但在那里它没有找到授权。所以我也添加/修改了这个:
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
options.Secure = CookieSecurePolicy.Always;
});
Run Code Online (Sandbox Code Playgroud)
这样,授权就起作用了,但在这个重定向 ITokenAcquisition 起作用之前,我无法获取令牌来调用下游 API,但现在当尝试获取令牌时,它会抛出异常。
因此,在我的控制器/服务中获取我修改和使用的令牌:
var accessToken = await _contextAccessor.HttpContext
.GetTokenAsync(OpenIdConnectDefaults.AuthenticationScheme, "access_token");
Run Code Online (Sandbox Code Playgroud)
现在,使用令牌,我将其添加到我的 HttpRequestMessage 中,如下所示:
request.Headers.Add("Authorization", $"Bearer {accessToken}");
Run Code Online (Sandbox Code Playgroud)
我在 StackOverflow 和 microsoft docs 上呆了 3 天,我不确定这是否都是“推荐”的,但这对我有用。
我在 Google Cloud Run 下运行 asp.net 应用程序时遇到了同样的问题,它终止了 TLS 连接。我收到错误:AADSTS50011:请求中指定的回复 URL 与为应用程序配置的回复 URL 不匹配。
我使用 fiddler 检查了对 login.microsoftonline.com 的请求,发现查询参数redirect_uri 与我在 Azure 应用程序中配置的 URL 完全匹配,只是它启动的是 http 而不是 https。
我最初尝试了涉及处理 OpenIdConnectEvents 事件和更新重定向 uri 的其他答案。这修复了对login.microsoftonline.com 的调用中的redirect_url 参数,然后它一直有效,直到我添加到图形API 中。然后我发现我网站的 Signin-oidc 页面会给出有关重定向 uri 不匹配的错误。这将导致它进入我的网站和 login.microsoftonline.com 之间的循环,反复尝试进行身份验证,直到最终登录失败。
经过进一步研究,ASP.net 提供了中间件来正确处理这种情况。您的 SSL 负载均衡器应将带有值 HTTPS 的标准标头 X-Forwarded-Proto 添加到请求中。它还应该发送带有原始 IP 地址的 X-Forwarded-For 标头,这对于调试、geoip 等很有用。
在 ASP.net 应用程序中,配置中间件:
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
Run Code Online (Sandbox Code Playgroud)
然后启用中间件:
app.UseForwardedHeaders();
Run Code Online (Sandbox Code Playgroud)
重要的是,您必须在调用依赖于它的 app.UseAuthentication/app.UseAuthorization 之前包含此内容。
来源: https: //learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer? view=aspnetcore-5.0
如果您的负载均衡器未添加 X-Forwarded-Proto 标头并且无法配置为这样做,则上面的文档概述了其他选项。