【问题标题】:Using azure AD B2C for blazor web api authentication使用 azure AD B2C 进行 blazor web api 身份验证
【发布时间】:2021-03-14 07:53:30
【问题描述】:

我一直在关注此文档,了解如何在 blazor Web 应用程序中使用 Azure AD B2C 进行身份验证 https://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/hosted-with-azure-active-directory-b2c?view=aspnetcore-5.0

遵循本文档后,我们得到了一个包含服务器和客户端的解决方案,两者都在 https 端口 5001 上运行。现在,我想切换到使用外部 api,而不是在端口上运行的5001.

当手动使用 blazor 检索到的访问令牌时,一切似乎都很好,并且身份验证成功。但是 blazor 只会自动将身份验证标头附加到以 https://localhost:5001 开头的请求。

当我改用 https://localhost:5003 时,身份验证标头为空。

有什么我可以添加到我的 MsalAuthentication 的提供程序选项中,以便它将此访问令牌传递给我在 https://localhost:5003 上运行的 api 吗?

builder.Services.AddHttpClient("{MyAssembly}.ServerAPI", client => client.BaseAddress = new Uri("https://localhost:5003"))
            .AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();

// Supply HttpClient instances that include access tokens when making requests to the server project
builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("{MyAssembly}.ServerAPI"));

builder.Services.AddMsalAuthentication(options =>
{
    builder.Configuration.Bind("AzureAdB2C", options.ProviderOptions.Authentication);
    options.ProviderOptions.DefaultAccessTokenScopes.Add("https://{myproject}.onmicrosoft.com/e3b857b7-df50-4633-ae02-df4d4b20e911/API.Access openid offline_access");
});

【问题讨论】:

    标签: azure blazor azure-ad-b2c msal


    【解决方案1】:

    如果您想向不在应用的基本 URI 中的 URI 发出传出请求,您可以创建一个自定义的 AuthorizationMessageHandler 类来实现它。更多详情请参考here

    例如

    创建自定义 AuthorizationMessageHandler 类

    using Microsoft.AspNetCore.Components;
    using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
    
    public class CustomAuthorizationMessageHandler : AuthorizationMessageHandler
    {
        public CustomAuthorizationMessageHandler(IAccessTokenProvider provider,
            NavigationManager navigationManager)
            : base(provider, navigationManager)
        {
            ConfigureHandler(
                authorizedUrls: new[] { "https://localhost:44389/" },
                scopes: new[] { "https://<>.onmicrosoft.com/api/user_impersonation" });
        }
    }
    

    在Program.cs中添加如下代码。

    using System;
    using System.Net.Http;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using System.Text;
    using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    
    namespace WebB2C
    {
        public class Program
        {
            public static async Task Main(string[] args)
            {
                var builder = WebAssemblyHostBuilder.CreateDefault(args);
                builder.RootComponents.Add<App>("app");
              
                builder.Services.AddScoped<CustomAuthorizationMessageHandler>();
                builder.Services.AddHttpClient("ServerAPI", client =>
                  client.BaseAddress = new Uri("https://localhost:44389/"))
                        .AddHttpMessageHandler<CustomAuthorizationMessageHandler>();
                builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>()
                 .CreateClient("ServerAPI"));
    
                builder.Services.AddMsalAuthentication(options =>
                {
                    builder.Configuration.Bind("AzureAdB2C", options.ProviderOptions.Authentication);
                    options.ProviderOptions.DefaultAccessTokenScopes.Add("https://<>.onmicrosoft.com/api/user_impersonation");
                    options.ProviderOptions.DefaultAccessTokenScopes.Add("openid");
                    options.ProviderOptions.DefaultAccessTokenScopes.Add("offline_access");
                });
    
                await builder.Build().RunAsync();
            }
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-03
      • 1970-01-01
      • 2017-04-08
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多