gpt4 book ai didi

asp.net - 将 AspNetIdentity 与 IdentityServer3 一起使用时,User.Identity.Name 始终为 null

转载 作者:行者123 更新时间:2023-12-02 16:48:07 27 4
gpt4 key购买 nike

这几天我一直在尝试使用 AspNetIdentity 设置新的 IdentityServer3。我可以使用现有的身份数据库登录,这一切都很好,但我永远无法让 User.Identity.Name 包含数据。我已尝试多次尝试添加自定义声明和范围以及向客户端添加范围。

最后,我加载了 IdentityServer3 示例存储库并使用 Webforms 客户端项目对其进行了测试,因为它已经在其“关于”页面中使用了 User.Identity.Name。

使用 WebForms 示例客户端 + AspNetIdentity 示例服务器 = User.Identity.Name 始终为 null使用 WebForms 示例客户端 + SelfHost 和 Seq 示例服务器 = User.Identity.Name 和数据我尝试过其他示例主机项目,它们都很好地填充了 User.Identity.Name 值。

现在,在客户端,我编写了一个解决方法来提取“preferred_username”声明值并用它设置“name”声明。

var id = new claimsIdentity(n.AuthenticationTicket.Identity.AuthenticationType);
id.AddClaims(userInfoResponse.GetClaimsIdentity().Claims);

//set the User.Identity.Name value
var name = id.Claims.Where(x => x.Type == "name").Select(x => x.Value).FirstOrDefault() ??
id.Claims.Where(x => x.Type == "preferred_username").Select(x => x.Value).FirstOrDefault();
id.AddClaim(new Claim("name", name));

我的问题是:

  1. 为什么 AspNetIdentity 包默认不填充这个?
  2. 我需要在服务器端进行哪些更改才能不需要更改客户端?

最佳答案

public static IEnumerable<ApiResource> GetApis()
{
return new ApiResource[]
{
new ApiResource("MyApi", "My Admin API")
{
UserClaims = { JwtClaimTypes.Name, JwtClaimTypes.Email }
}
};
}

在 Identityserver4 中,您可以将 UserClaims 添加到您的资源中。帮我修好了。

关于asp.net - 将 AspNetIdentity 与 IdentityServer3 一起使用时,User.Identity.Name 始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42441771/

27 4 0