【发布时间】:2021-06-19 15:11:29
【问题描述】:
我正在使用 identityserver4 进行身份验证,它的布局如下:identity server4 -> Web Api -> Blazor WASM Client(Standalone)。一切都经过身份验证并且运行良好。我一直得到经过身份验证的用户声明到 wasm 客户端。 我现在正在尝试添加更多直接来自数据库的声明。我本可以将声明添加到 identityserver 令牌,但令牌变得太大(> 2kb),然后 identityserver 停止工作。显然这是一个已知问题。
所以我想建立授权并尽量保持来自身份服务器的 jwt 令牌很小。
在 program.cs 文件中,我有一个这样的 http 客户端
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddOidcAuthentication(options =>// 在此处配置您的身份验证提供程序选项。 // 更多信息见https://aka.ms/blazor-standalone-auth //builder.Configuration.Bind("Local", options.ProviderOptions); ...提供者选项
options.ProviderOptions.ResponseType = "code";
options.UserOptions.RoleClaim = "role";
}).AddAccountClaimsPrincipalFactory<CustomAccountClaimsPrincipalFactory>();
await builder.Build().RunAsync();
在文件 CustomAccountClaimsPrincipalFactory 我有这个
public class CustomAccountClaimsPrincipalFactory
: AccountClaimsPrincipalFactory<RemoteUserAccount>
{
private const string Planet = "planet";
[Inject]
public HttpClient Http { get; set; }
public CustomAccountClaimsPrincipalFactory(IAccessTokenProviderAccessor accessor)
: base(accessor) {
}
public async override ValueTask<ClaimsPrincipal> CreateUserAsync(
RemoteUserAccount account,
RemoteAuthenticationUserOptions options)
{
var user = await base.CreateUserAsync(account, options);
if (user.Identity.IsAuthenticated)
{
var identity = (ClaimsIdentity)user.Identity;
var claims = identity.Claims.Where(a => a.Type == Planet);
if (!claims.Any())
{
identity.AddClaim(new Claim(Planet, "mars"));
}
//get user roles
//var url = $"/Identity/users/112b7de8-614f-40dc-a9e2-fa6e9d2bf85a/roles";
var dResources = await Http.GetFromJsonAsync<List<somemodel>>("/endpoint");
foreach (var item in dResources)
{
identity.AddClaim(new Claim(item.Name, item.DisplayName));
}
}
return user;
}
}
这不起作用,因为调用它时 httpclient 不是 biolt 并且 http 客户端使用构建基本 http 客户端的相同构建器。
我如何让它工作?
【问题讨论】:
标签: api blazor identityserver4 webassembly