我正在关注 Identity Server 快速入门模板,并尝试设置以下内容
- 身份服务器 aspnet 核心应用程序
- Mvc 客户端,它向 is4 进行身份验证,并调用 webapi 客户端,它是一个受保护的 api 资源。
有一个额外的ApplicationUser列,我添加到ProfileService这样的声明中:
public async Task GetProfileDataAsync(ProfileDataRequestContext context)
{
var sub = context.Subject.GetSubjectId();
var user = await _userManager.FindByIdAsync(sub);
if (user == null)
return;
var principal = await _claimsFactory.CreateAsync(user);
if (principal == null)
return;
var claims = principal.Claims.ToList();
claims.Add(new Claim(type: "clientidentifier", user.ClientId ?? string.Empty));
// ... add roles and so on
context.IssuedClaims = claims;
}
最后是 Mvc Client appConfigureServices方法中的配置:
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
}).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.ClientId = "mvc";
options.ClientSecret = "mvc-secret";
options.ResponseType = "code";
options.SaveTokens = true;
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("offline_access");
options.Scope.Add("api1");
options.GetClaimsFromUserInfoEndpoint = true;
options.ClaimActions.MapUniqueJsonKey("clientidentifier", "clientidentifier");
});
GetClaimsFromUserInfoEndpoint设置为true我可以访问 中的自定义声明,User.Identity但这会导致 2 次调用ProfileService。
如果我删除或设置为 false,则此声明仍然是 access_token 的一部分,但不是 id_token 的一部分,然后我无法从上下文用户访问此特定声明。
有没有更好的方法可以从用户主体访问此声明而不会导致 2 次调用(就像现在一样)?或者也许从上下文中读取 access_token 并在检索到令牌后更新用户声明?
谢谢 :)