从 Blazor WASM 中的 AccountClaimsPrincipalFactory 调用 API

jus*_*ugh 6 c# asp.net blazor blazor-webassembly

我将 Blazor WASM 与 AzureB2C 结合使用来调用 Azure Functions 中托管的 API。我想在成功登录时调用我的 API,以将用户信息添加/更新到数据库中。我一直在遵循这个指南。当尝试将我键入的 httpclient 注入 AccountClaimsPrincipalFactory 时,我遇到了运行时错误:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] 未处理的异常呈现组件:ValueFactory 尝试访问此实例的 Value 属性。System.InvalidOperationException: ValueFactory 尝试访问此实例的 Value 属性。

这显示在浏览器中,但应用程序编译并运行得很好。如果我不注入我的代码,代码会很好用PlatformServiceClient,但我需要进行 API 调用来记录用户。涉及以下文件。我调整了一些东西以简化。这似乎是合适的方法,但我还没有看到在声明工厂中进行 api 调用的示例。

CustomAccountFactory.cs

 public class CustomAccountFactory: AccountClaimsPrincipalFactory<CustomUserAccount>
    {
        public IPlatformServiceClient client { get; set; }
        public CustomAccountFactory(NavigationManager navigationManager,
            IPlatformServiceClient platformServiceClient,
            IAccessTokenProviderAccessor accessor) : base(accessor)
        {
            client = platformServiceClient;
        }

        public override async ValueTask<ClaimsPrincipal> CreateUserAsync(
            CustomUserAccount account, RemoteAuthenticationUserOptions options)
        {
            var initialUser = await base.CreateUserAsync(account, options);

            if (initialUser.Identity.IsAuthenticated)
            {
                //call the API here
                await client.RegisterUserAsync();
                
            }

            return initialUser;
        }
    }
Run Code Online (Sandbox Code Playgroud)

Program.cs摘抄

builder.Services.AddScoped<CustomAuthorizationMessageHandler>();

builder.Services.AddHttpClient<IPlatformServiceClient, PlatformServiceClient>(
    client => client.BaseAddress = new Uri(builder.Configuration["PlatformServiceUrl"]))
.AddHttpMessageHandler<CustomAuthorizationMessageHandler>();

builder.Services.AddMsalAuthentication<RemoteAuthenticationState, CustomUserAccount>(options =>
{
    builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
    options.ProviderOptions.DefaultAccessTokenScopes.Add("openid");
    options.ProviderOptions.DefaultAccessTokenScopes.Add("offline_access");
    options.ProviderOptions.DefaultAccessTokenScopes.Add("access_as_user");
    options.ProviderOptions.LoginMode = "redirect";
    options.UserOptions.RoleClaim = "roles";
}).AddAccountClaimsPrincipalFactory<RemoteAuthenticationState, CustomUserAccount, CustomAccountFactory>();

Run Code Online (Sandbox Code Playgroud)

CustomAuthorizationMessageHandler.cs

 public class CustomAuthorizationMessageHandler : AuthorizationMessageHandler
    {
        public CustomAuthorizationMessageHandler(IAccessTokenProvider provider,
            NavigationManager navigationManager)
            : base(provider, navigationManager)
        {
            ConfigureHandler(
                authorizedUrls: new[] { "http://localhost:7071" },
                scopes: new[] { "access_as_user" });
        }
    }
Run Code Online (Sandbox Code Playgroud)

jus*_*ugh 7

我通过创建客户端的命名实例并将 IHttpClientFactory 传递到 CustomAccountFactory 中解决了这个问题。

builder.Services.AddHttpClient<PlatformServiceClient>("PlatformServiceClient",
    client => client.BaseAddress = new Uri(builder.Configuration["PlatformServiceUrl"]))
.AddHttpMessageHandler<CustomAuthorizationMessageHandler>();
Run Code Online (Sandbox Code Playgroud)

我可以在那里创建一个客户端,但我必须手动设置我的网址,而不是使用我已经完成这项工作的键入客户端。

var client = factory.CreateClient("PlatformServiceClient");
var response = await client.GetAsync("/user/me");
Run Code Online (Sandbox Code Playgroud)

我还在调用 AddMsalAuthenication 之前注册了新客户端

builder.Services.AddTransient(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("PlatformServiceClient"));

我按照Coding Flamingo在这里找到的代码完成了所有这些操作。一切都按预期进行。

  • 为我工作。感谢这篇文章。请注意,它适用于 CustomAuthenticationStateProvider 或 CustomAccountClaimsPrincipalFactory。 (2认同)