Microsoft Identity Web:更改重定向URI [英] Microsoft Identity Web: Change Redirect Uri

查看:45
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆