Asp.net mvc 无法从Identity Server 3获取用户信息
我正试图遵循使用混合流客户端创建Identity Server的教程。在我通过用户信息端点获取用户信息之前,一切都很好Asp.net mvc 无法从Identity Server 3获取用户信息,asp.net-mvc,identityserver3,openid-connect,Asp.net Mvc,Identityserver3,Openid Connect,我正试图遵循使用混合流客户端创建Identity Server的教程。在我通过用户信息端点获取用户信息之前,一切都很好 Notifications = new OpenIdConnectAuthenticationNotifications { AuthorizationCodeReceived = async n => { var userInfoClient = new UserInfoClient(new Uri(Constants.UserInfoE
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,区域性=中性,
PublicKeyToken=b77a5c561934e089]“不可分配给参数类型
“[mscorlib,版本=4.0.0.0,
文化=中性,PublicKeyToken=b77a5c561934e089]'
另一个,当指向方法名称时,会显示:
类型“Claim”在未引用的程序集中定义。你
必须添加对程序集“System.Security.Claims”的引用,
版本=4.0.1.0,区域性=中性,PublicKeyToken=503f5..'
我试图通过nuget添加对此程序集的引用,但随后又发生了另一个冲突。IdentityProvider和mscorlib之间对System.Security.Claims的引用不明确
如何才能实现我的目标,将用户声明分配给登录用户的身份?查看教程,这一行:
identity.AddClaims(userInfoResponse.Claims);
必须使用以下命令更改:
identity.AddClaims(userInfoResponse.GetClaimsIdentity().Claims);
第二个编译器错误是使用Claim
type而没有指定名称空间。明确标识您在哪里使用声明
,并向其中添加正确的命名空间
注意:
这:userInfoResponse.GetClaimsIdentity().Claims
相当于:
userInfoResponse.Claims.Select(c => new System.Security.Claim(c.Item1, c.Item2))
UserInformResponse不包含“GetClaimSideEntity”的定义。GetClaimSideEntity
是一个。我怀疑您没有为IdentityModel
使用正确的nuget包。请注意,有两个不同的软件包:Thinktecture.IdentityModel.Client
,这是不推荐使用的软件包;IdentityModel
(版本2.8.0),这是正确的软件包,但在较新版本中不包含扩展方法。我使用的是“OpenID Connect&OAuth 2.0客户端库”,IdentityModel的2.8.0版您所参考的教程使用的是旧版本的库(可能是1.X
分支)。您必须使用旧版本或相应地更改代码(在2.8.0版声明中
属性应已返回一个IEnumerable
)