C# Microsoft标识Web:更改重定向Uri
我正在使用.net 5,Identity Web Ui访问Microsoft Graph。在哪里可以配置重定向URI 我需要指定完整的Uri,因为从callbackUri生成的Uri不正确,因为它位于具有SSL卸载的负载平衡器后面 这是我当前的配置服务部分C# Microsoft标识Web:更改重定向Uri,c#,azure-active-directory,microsoft-graph-api,msal,microsoft-identity-web,C#,Azure Active Directory,Microsoft Graph Api,Msal,Microsoft Identity Web,我正在使用.net 5,Identity Web Ui访问Microsoft Graph。在哪里可以配置重定向URI 我需要指定完整的Uri,因为从callbackUri生成的Uri不正确,因为它位于具有SSL卸载的负载平衡器后面 这是我当前的配置服务部分 services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApp(Config
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以发出请求
然后我需要AzureAd/B2C登录重定向到带有前门url的uri:
事情就这样破裂了。我是这样解决的:
request.Headers.Add("Authorization", $"Bearer {accessToken}");
首先,您必须将此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();
}
};
});
现在,我使用令牌将其添加到我的HttpRequestMessage中,如下所示:
request.Headers.Add("Authorization", $"Bearer {accessToken}");
我在StackOverflow和microsoft docs上生活了3天,我不确定这是否都是“推荐的”但这对我很有效。你的意思是要在应用程序注册端配置重定向url吗?@AllenWu我需要将我的应用程序发送的url配置为回调url,以便它与注册中的url匹配。你做到了吗?@david ao No,启用了从负载平衡器到web服务器的SSL。这恰好是我的生产配置。我仍然想知道答案。我为一个类似的问题挣扎了三天,我今天在工作中成功地做到了,我将发布一个令人惊讶的答案。我能够启用SSL并完成我的工作,但我会接受这是解决这一问题的正确方法。
request.Headers.Add("Authorization", $"Bearer {accessToken}");