我正在使用Microsoft.IdentityModel.dll
Set & Get 中的声明WCF
。我还实施MessageInspectors
了设置索赔。因此,我正在ClaimsIdentity
从客户端添加如下请求标头。
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
{
var claims = new List<Claim> { new Claim(UserIdClaim, "12345"), };
ClaimsIdentity claimsIdentity = new ClaimsIdentity(claims);
MessageHeader<ClaimsIdentity> header = new MessageHeader<ClaimsIdentity>(claimsIdentity);
var untypedHeader = header.GetUntypedHeader(ClaimsName, ClaimsNameSpace);
request.Headers.Add(untypedHeader);
return null;
}
而服务端,
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
ClaimsIdentity claimsIdentity = request.Headers.GetHeader<ClaimsIdentity>(ClaimsName, ClaimsNameSpace);
var claimsIdentitylst = new ClaimsIdentityCollection(new List<IClaimsIdentity> { claimsIdentity });
IClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(claimsIdentitylst);
Thread.CurrentPrincipal = claimsPrincipal;
return null;
}
我正在尝试访问方法 implementation( OperationContract
) 中 的声明值,AfterReceiveRequest
如下所示。但索赔在 中不可用Thread.CurrentPrincipal
。
var userIdClaim = ((IClaimsIdentity)Thread.CurrentPrincipal.Identity).Claims.First(c => c.ClaimType == UserIdClaim);
userIdClaim
此处为空。
有任何想法吗?