无法从Identity Server 3获取用户信息
问题描述:
我正在尝试遵循this关于创建Identity Server与ha混合流客户端的教程。直到我通过用户信息端点获取用户信息时,这一切都很好。无法从Identity Server 3获取用户信息
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthorizationCodeReceived = async n =>
{
var userInfoClient = new UserInfoClient(new Uri(Constants.UserInfoEndpoint).ToString());
var userInfoResponse = await userInfoClient.GetAsync(n.ProtocolMessage.AccessToken);
var identity = new ClaimsIdentity(n.AuthenticationTicket.Identity.AuthenticationType);
identity.AddClaims(userInfoResponse.Claims);
n.AuthenticationTicket = new AuthenticationTicket(identity, n.AuthenticationTicket.Properties);
}
}
我在点得到一个编译错误,其中添加的声明:identity.AddClaims(userInfoResponse.Claims);
的错误区域实际上是两个。的时,当指向参数表示:
参数类型 'System.Collections.Genereic.IEnumerable [mscorlib程序,版本= 4.0.0.0,文化=中性 公钥= b77a5c561934e089]' 不是分配给参数类型 '[mscorlib程序,版本= 4.0.0.0, 文化=中性公钥= b77a5c561934e089]'
而另外一个,指向方法名时说:
'声明'类型是在未引用的程序集中定义的。您 必须到集添加引用“System.Security.Claims, 版本= 4.0.1.0,文化=中性公钥= 503f5 ..”
我试图通过的NuGet添加引用到这个组件,但那么又发生了另一场冲突。 IdentityProvider和mscorlib之间的System.Security.Claims有不明确的参考。
如何实现为用户声明分配登录用户身份的目标?
答
看着教程,这条线:
identity.AddClaims(userInfoResponse.Claims);
必须将其改为:通过使用Claim
类型的出现
identity.AddClaims(userInfoResponse.GetClaimsIdentity().Claims);
你的第二个编译错误,不指定的命名空间。确定您正在使用Claim
的位置,并为其添加正确的名称空间。
注:
此:userInfoResponse.GetClaimsIdentity().Claims
等效于:
userInfoResponse.Claims.Select(c => new System.Security.Claim(c.Item1, c.Item2))
userInfoResponse不包含一个定义 'GetClaimsIdentity' – user2128702
'GetClaimsIdentity'是一个[扩展方法](HTTPS:/ /github.com/IdentityModel/IdentityModel/blob/master/source/IdentityModel.Net45/Client/UserInfoResponseExtensions.cs)。我怀疑你没有为'IdentityModel'使用正确的nuget包。请注意,有两种不同的软件包:'Thinktecture.IdentityModel.Client',它是已弃用的软件包和'IdentityModel'(版本2.8.0),它是正确的,但在新版本中不包含扩展方法。 –
我正在使用'OpenID Connect&OAuth 2.0客户端库',版本2.8.0 IdentityModel – user2128702