为什么在使用授予密码和授权码请求令牌时,我没有得到相同的声明?
Why when requesting a token using the grant password and authorization code, I do not get the same claims?
我设置了一个 Identity Server 客户端,可以使用密码和授权码授权,我可以同时使用这两者,但是在查看令牌时,它们不包含相同的声明,这是它的假设吗工作还是我缺少一些配置?
如果这是它的工作原理(每次授权都有不同的声明),那么在使用密码授权时,我应该使用配置文件服务来添加其他声明吗?
您需要实施 IResourceOwnerPasswordValidator
,并且 return 您需要的声明列表。默认实现仅发送 sub
声明。
请参阅 IdentityServer 存储库中的 the example implementation for ASP.NET Core Identity。
然后修改它以发送额外的声明。或者使用 ProfileService
来填充它:
public virtual async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
{
var user = await _userManager.FindByNameAsync(context.UserName);
if (user != null)
{
var result = await _signInManager.CheckPasswordSignInAsync(user, context.Password, true);
if (result.Succeeded)
{
var sub = await _userManager.GetUserIdAsync(user);
_logger.LogInformation("Credentials validated for username: {username}", context.UserName);
// return additional claims
var claims = await _userManager.GetClaimsAsync(user);
context.Result = new GrantValidationResult(sub, AuthenticationMethods.Password, claims);
return;
}
// ... see the link above for a full implementation
}
您还可以创建一个新的 ClaimsPrincipal
来填充结果。有关其他选项,请参阅 GrantValidationResult
构造函数重载。
我设置了一个 Identity Server 客户端,可以使用密码和授权码授权,我可以同时使用这两者,但是在查看令牌时,它们不包含相同的声明,这是它的假设吗工作还是我缺少一些配置?
如果这是它的工作原理(每次授权都有不同的声明),那么在使用密码授权时,我应该使用配置文件服务来添加其他声明吗?
您需要实施 IResourceOwnerPasswordValidator
,并且 return 您需要的声明列表。默认实现仅发送 sub
声明。
请参阅 IdentityServer 存储库中的 the example implementation for ASP.NET Core Identity。
然后修改它以发送额外的声明。或者使用 ProfileService
来填充它:
public virtual async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
{
var user = await _userManager.FindByNameAsync(context.UserName);
if (user != null)
{
var result = await _signInManager.CheckPasswordSignInAsync(user, context.Password, true);
if (result.Succeeded)
{
var sub = await _userManager.GetUserIdAsync(user);
_logger.LogInformation("Credentials validated for username: {username}", context.UserName);
// return additional claims
var claims = await _userManager.GetClaimsAsync(user);
context.Result = new GrantValidationResult(sub, AuthenticationMethods.Password, claims);
return;
}
// ... see the link above for a full implementation
}
您还可以创建一个新的 ClaimsPrincipal
来填充结果。有关其他选项,请参阅 GrantValidationResult
构造函数重载。