配置服务 - soul-soft/IdentityServer GitHub Wiki
用于签发token的claims,以及用户信息的claims
用于返回用户是否激活,如果token的类型为jwt的话,只在调用token端点时验证(因为jwt的机制不是由identityserver统一验证的),如果token的类型为reference时,在用户登入以及调用introspection端点时都会验证。
当调用者为ProfileDataCallers.TokenEndpoint返回的claim将写的token中,并且严格验证范围是否允许
当调用者为ProfileDataCallers.UserInfoEndpoint返回的claim将作为用户信息返回,不验证验证范围是否允许
配置
//客户端配置
new Client()
{
ClientId = "client3",
//AccessTokenType = AccessTokenType.Reference,
AllowedGrantTypes = new []
{
GrantTypes.ClientCredentials,
GrantTypes.RefreshToken,
GrantTypes.Password,
"myGrant"
},
ClientSecrets = new Secret[]
{
new Secret("secret".Sha512())
},
AllowedScopes = new[]
{
"openid",
"api"
}
}
//api scope
new ApiScope("api")
{
//准许role
ClaimTypes = new string[]
{
JwtClaimTypes.Role
},
}
new IdentityResource("openid")
{
//准许sub
ClaimTypes = new string[]
{
JwtClaimTypes.Subject
}
}
实现
public class ProfileService : IProfileService
{
public Task<IEnumerable<Claim>> GetProfileDataAsync(ProfileDataRequestContext conte
{
var claims = new List<Claim>();
//request.Subject是授权验证返回的一些claim
//获取授权时填写的用户id,可以通过这个id来读取数据库中的用户,来配置cliams
var sub = request.Subject.GetSubject();
//token端点
if (context.Caller == ProfileDataCallers.TokenEndpoint)
{
//context.ClaimTypes值得就是客户端准许的claim
//只有准许的claim才能签发到token中
if (context.ClaimTypes.Contains(JwtClaimTypes.Subject))
{
claims.Add(new Claim(JwtClaimTypes.Subject, "10"));
}
if (context.ClaimTypes.Contains(JwtClaimTypes.Role))
{
claims.Add(new Claim(JwtClaimTypes.Role, "admin"));
}
}
//用户信息端点
if (context.Caller == ProfileDataCallers.UserInfoEndpoint)
{
//判断是否准许
if (context.ClaimTypes.Contains(JwtClaimTypes.Subject))
{
profiles.Add(new Claim(JwtClaimTypes.Subject, "10"));
}
//不准许也能签发
profiles.Add(new Claim(JwtClaimTypes.Role, "1"));
}
return Task.FromResult<IEnumerable<Claim>>(claims);
}
public Task<bool> IsActiveAsync(IsActiveContext context)
{
return Task.FromResult(true);
}
}