4

我正在使用 .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();
4

3 回答 3

3

我遇到了一个仅在前门后面暴露的 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: 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 天,我不确定这是否都是“推荐的”,但这对我有用。

于 2021-04-28T19:14:38.187 回答
1

我面临着类似的问题 3 天。下面的代码帮助我摆脱了这个问题。

string[] initialScopes = Configuration.GetValue<string>("CallApi:ScopeForAccessToken")?.Split(' ');

        services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAd")
            .EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
            .AddInMemoryTokenCaches();
        services.AddControllers();
        services.AddRazorPages().AddMvcOptions(options =>
        {
            var policy = new AuthorizationPolicyBuilder()
            .RequireAuthenticatedUser().Build();
            options.Filters.Add(new AuthorizeFilter(policy));

        }).AddMicrosoftIdentityUI();
        services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
        {
            options.SaveTokens = true; // this saves the token for the downstream api
            options.Events = new OpenIdConnectEvents
            {
                OnRedirectToIdentityProvider = async ctxt =>
                {

                    ctxt.ProtocolMessage.RedirectUri = "https://example.org/signin-oidc";
                    await Task.Yield();
                }
            };
        });
于 2022-01-12T15:41:20.827 回答
1

我在 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();
 });

然后启用中间件:

app.UseForwardedHeaders();

重要的是,您必须在调用依赖它的 app.UseAuthentication/app.UseAuthorization 之前包含它。

来源:https ://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-5.0

如果您的负载均衡器没有添加 X-Forwarded-Proto 标头并且无法配置为这样做,那么上面的文档概述了其他选项。

于 2022-03-01T11:02:09.740 回答