【发布时间】:2022-02-02 00:25:28
【问题描述】:
我正在将 Blazor WASM 与 AzureB2C 一起使用来调用托管在 Azure Functions 中的 API。我想在成功登录时调用我的 API 以将用户信息添加/更新到数据库中。我一直在关注this guide。当尝试将我输入的 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;
}
}
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>();
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" });
}
}
【问题讨论】:
-
好吧...异常表明
Lazy<>.CreateValue()正在发生递归——这可能会抑制异常的实际原因。我将关联此链接以供考虑:docs.microsoft.com/en-us/answers/questions/447967/… 这似乎很相关(于 2021 年 6 月提出);它提供了重现步骤,似乎表明CustomAuthorizationMessageHandler是一个问题(就您的样本而言) -
您的
CustomAuthorizationMessageHandler是什么样的?它与您引用的文档相同吗? docs.microsoft.com/en-us/aspnet/core/blazor/security/… -
感谢@BrettCaswell。我添加了消息处理程序代码。这是非常基本的。我以前看过那个帖子,这肯定是我收到的错误消息。但是,我可以将此问题隔离到将 httpclient 注入到声明工厂中。如果我移除注射。每个人都工作正常,但我无法对 API 进行经过身份验证的调用。我可能会做一个解决方法,但这似乎是正常的用例。我希望有人已经做到了。
标签: c# asp.net blazor blazor-webassembly