【发布时间】:2017-07-15 11:17:20
【问题描述】:
几天来,我一直在尝试使用 AspNetIdentity 设置新的 IdentityServer3。我可以使用现有的身份数据库登录,这一切都很好,但我永远无法让 User.Identity.Name 包含数据。 我已尝试多次尝试添加自定义声明和范围以及向客户端添加范围。
最后,我加载了 IdentityServer3 示例存储库并使用 webforms 客户端项目对其进行了测试,因为它已经在其“关于”页面中使用了 User.Identity.Name。
使用 WebForms 示例客户端 + AspNetIdentity 示例服务器 = User.Identity.Name 始终为空 使用 WebForms 示例客户端 + SelfHost 和 Seq 示例服务器 = User.Identity.Name 和数据 我已经尝试过其他示例宿主项目,它们都可以很好地填充 User.Identity.Name 值。
现在,在客户端,我编写了一个解决方法来提取“preferred_username”声明值并使用它设置“名称”声明。
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));
我的问题是:
- 为什么 AspNetIdentity 包默认不填充这个?
- 我需要在服务器端进行哪些更改,以便无需更改客户端?
【问题讨论】:
-
你有没有找到解决这个问题的“更好”的解决方案?
-
我们将保留该解决方案。有用。我们当时还添加了其他自定义声明。我们没有足够的时间深入研究我发布的示例之间的差异。
-
@TheBrian 变量
userInfoResponse如何在上述解决方法中获得它的值?
标签: asp.net asp.net-identity identityserver3