如何解决Microsoft Identity Web:更改重定向 Uri
我正在使用 .net 5、Identity Web Ui 来访问 Microsoft Graph。在哪里可以配置我的重定向 URI?
我需要指定完整的 Uri,因为 callbackUri 生成的 Uri 不正确,因为它位于具有 SSL 卸载的负载均衡器之后。
这是我当前的 ConfigureServices 部分
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"))
.EnabletokenAcquisitionToCallDownstreamApi(initialScopes)
.AddMicrosoftGraph(Configuration.GetSection("DownstreamApi"))
.AddInMemoryTokenCaches();
解决方法
我遇到了类似的问题,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();
我尝试了 AddDownstreamWebApi,但没有成功,所以我只是使用 ITokenAcquisition 获取所需的令牌并将其添加到 HttpClient 以发出我的请求。
然后我需要使用前门 url 将 AzureAd/B2C 登录重定向到 uri: https://example.org/signin-oidc 然后事情就坏了。我是这样解决的:
首先,您必须将此 url 添加到您在 azure 门户中的应用程序注册中,非常重要的是区分大小写,它关心尾部斜杠,我怀疑有许多 url 指向相同的控制器,并且它们的顺序有一些影响,我只是删除了所有内容并保留了最低限度。
然后在配置服务方法中:
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();
}
};
});
这样重定向就起作用了,但我在受保护的页面和 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;
});
这样授权就起作用了,但是我无法获取令牌来调用下游 API,在此重定向之前 ITokenAcquisition 起作用,但是现在在尝试获取令牌时会引发异常。
所以在我的控制器/服务中获取我修改和使用的令牌:
var accessToken = await _contextAccessor.HttpContext
.GetTokenAsync(OpenIdConnectDefaults.AuthenticationScheme,"access_token");
所以现在使用令牌将其添加到我的 HttpRequestMessage 中,如下所示:
request.Headers.Add("Authorization",$"Bearer {accessToken}");
我在 StackOverflow 和 microsoft docs 上呆了 3 天,我不确定这是否都是“推荐”的,但这对我有用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。