我想在我的一个声明中将一个int数组放在一个Web应用程序.net core 2.2上。
登录以创建票证时,我使用它来添加声明,但是如何添加复杂对象。
if (ticket.HasScope(OpenIdConnectConstants.Scopes.Profile))
{
if (!string.IsNullOrWhiteSpace(user.FirstName))
identity.AddClaim(CustomClaimTypes.FirstName, user.FirstName, OpenIdConnectConstants.Destinations.IdentityToken);
if (!string.IsNullOrWhiteSpace(user.LastName))
identity.AddClaim(CustomClaimTypes.LastName, user.LastName, OpenIdConnectConstants.Destinations.IdentityToken);
if (user.Functions.Any())
// not possible : Functions = List<int>
identity.AddClaim(CustomClaimTypes.Functions, user.Functions, OpenIdConnectConstants.Destinations.IdentityToken);
}
使用AddClaims,只能添加字符串
您可以重复添加相同的声明类型,例如:
foreach(var f in user.Functions)
identity.AddClaim(CustomClaimTypes.Functions, f.ToString(), OpenIdConnectConstants.Destinations.IdentityToken);
作为替代方案,您可以在访问声明后加入整数并拆分它们:
if (user.Functions.Any())
{
var joinedFunctions = string.Join(";", user.Functions);
identity.AddClaim(CustomClaimTypes.Functions, joinedFunctions, OpenIdConnectConstants.Destinations.IdentityToken);
}
要检索值,您可以在以后拆分它们:
functionsClaimValue.split(';');
您需要确保您选择的分隔符(在此示例中为分号)不能作为常规字符包含在值中。
您可以将复杂对象序列化为json,并将其添加到声明中。有点像:
identity.AddClaim(ClaimName, JsonConvert.SerializaObject(intArray));
然后在读取时将其反序列化:
int[] intArray = JsonConvert.DeserializeObject<int[]>(claim.Value);