Microsoft Identity Web: Change Redirect Uri(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();
推荐答案
我遇到了类似的问题,一个Web应用程序只在前门后面暴露,该Web应用程序必须调用自定义的下游WebApi。
我在localhost dev计算机上工作的服务配置:
// 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();
我尝试了AddDownstream WebApi,但未能使其工作,所以我只是使用ITokenAcquisition获取了所需的令牌,并将其添加到HttpClient以发出我的请求。
然后我需要AzureAd/B2C登录,重定向到带有前门url的uri: https://example.org/signin-oidc然后东西就断了。我是这样解决的:首先,您必须将此URL添加到您在Azure门户中的应用注册中,非常重要的一点是它区分大小写,它关心尾随斜杠,我怀疑有许多URL指向完全相同的控制器,这些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;
});
这样授权起作用了,但是在这个重定向ITokenAcquisition起作用之前,我无法让令牌调用下游API,但现在尝试获取令牌时抛出异常。
因此在我的控制器/服务中,要获取我修改和使用的令牌:
var accessToken = await _contextAccessor.HttpContext
.GetTokenAsync(OpenIdConnectDefaults.AuthenticationScheme, "access_token");
现在我使用令牌将其添加到我的HttpRequestMessage中,如下所示:
request.Headers.Add("Authorization", $"Bearer {accessToken}");
我在StackOverflow和Microsoft Docs上生活了3天,我不确定这是所有推荐的&q;,但这对我有效。
这篇关于Microsoft Identity Web:更改重定向URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Microsoft Identity Web:更改重定向URI


基础教程推荐
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01