我正在使用来自 msdn 的 CustomToken 示例,并且想着添加新声明并重建先前制作的令牌或将其拆分,添加新值并制作新令牌的想法。我尝试向依赖方中的委托人添加新声明但不起作用,我尝试重建令牌并在 CustomeSecurityTokenServiceConfiguration 上出现错误“已添加具有相同密钥的项目”并且我尝试了近十几个没有结果的不同方法。关于如何做到这一点的任何想法,或者我是否正在寻找不存在的东西?
尝试依赖默认值
if (claimsPrincipal != null)
{
ClaimsIdentity identity = claimsPrincipal.Identity as ClaimsIdentity;
BootstrapContext bootstrapContext = identity.BootstrapContext as BootstrapContext;
var newclaims = new List<Claim>
{
new Claim(ClaimTypes.PostalCode,"56789")
};
foreach(Claim claim in claimsPrincipal.Claims)
{
newclaims.Add(claim);
}
var claimsIdentity = new ClaimsIdentity(newclaims, "Name", "UserName", ClaimTypes.Role);
ClaimsPrincipal pass = new ClaimsPrincipal(claimsIdentity);
FederatedPassiveSecurityTokenServiceOperations.ProcessRequest(Request, pass, SimpleWebToken.CustomSecurityTokenServiceConfiguration.Current.CreateSecurityTokenService(), Response);
//SimpleWebToken.SimpleWebTokenHandler Added = new SimpleWebToken.SimpleWebTokenHandler();
//Added.AddtoCreatedToken(pass);
}
我也尝试添加我自己的创建令牌,即
public SecurityToken AddtoCreatedToken(ClaimsPrincipal claimsPrincipal)
{
if (claimsPrincipal == null)
{
throw new ArgumentNullException("tokenDescriptor");
}
NameValueCollection properties = new NameValueCollection();
foreach (Claim claim in claimsPrincipal.Claims)
{
properties.Add(claim.Type, claim.Value);
}
SimpleWebToken token = new SimpleWebToken(properties);
return token;
}