gpt4 book ai didi

c# - 我的 IUserClaimsPrincipalFactory 实现导致 IdentityServer4 上出现 StackOverflowException

转载 作者:行者123 更新时间:2023-12-01 23:57:56 24 4
gpt4 key购买 nike

我在 Asp.NET Core 上使用 IdentityServer4 和 Asp.NET Core Identity 构建了一个身份服务器。我想将 ApplicationUser 的属性映射到客户端访问 UserInfoEndpoint 时发送的声明。

我尝试按如下方式实现 IUserClaimsPrincipalFactory:

public class CustomUserClaimsPrincipalFactory : IUserClaimsPrincipalFactory<ApplicationUser>
{

public async Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
{
var principal = await CreateAsync(user);
((ClaimsIdentity)principal.Identity).AddClaims(new[] {
new Claim(ClaimTypes.GivenName, user.FirstName),
new Claim(ClaimTypes.Surname, user.LastName),

});
return principal;
}
}

并像这样注册:

services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders()
.AddClaimsPrincipalFactory<CustomUserClaimsPrincipalFactory>();

但是当客户端尝试访问 UserInfoEndpoint 时,我收到 StackOverflowException。

你能帮我解决这个问题吗?

注意:我测试了它,当我不注册 ClaimsPrincipal 工厂时,我没有收到任何错误。

最佳答案

这行不是递归的,函数在无限循环中递归调用自身

var principal = await CreateAsync(user);

CreateUser 是您所在的函数,您再次递归调用它,这会创建无限循环,因此堆栈溢出

关于c# - 我的 IUserClaimsPrincipalFactory 实现导致 IdentityServer4 上出现 StackOverflowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41524313/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com