【发布时间】:2019-01-24 19:26:43
【问题描述】:
当我使用 oidc 进行身份验证时,我会收到一堆声明。如果我不添加我的自定义 IProfileService 所有这些声明都在身份服务器发出的 id_token 中传递。如果我提供自己的 ProfileService,则主题上的声明列表是从 idp 返回的内容的子集。有什么方法可以在配置文件服务中获取完整列表?
这是来自 Startup.cs 的相关信息:
var builder = services.AddIdentityServer(options =>
{
options.Events.RaiseErrorEvents = true;
options.Events.RaiseInformationEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseSuccessEvents = true;
}).AddProfileService<ProfileService>();
services.AddAuthentication()
.AddOpenIdConnect("Name", "Name", o =>
{
o.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
o.SignOutScheme = IdentityServerConstants.SignoutScheme;
o.Authority = "https://sub.domain.com/adfs/";
o.ClientId = "00000000-0000-0000-0000-000000000000";
o.ClientSecret = "secret";
o.ResponseType = "id_token";
o.SaveTokens = true;
o.CallbackPath = "/signin-adfs";
o.SignedOutCallbackPath = "/signout-callback-adfs";
o.RemoteSignOutPath = "/signout-adfs";
o.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "role"
};
});
还有我的 ProfileService:
public class ProfileService : IProfileService
{
public Task GetProfileDataAsync(ProfileDataRequestContext context)
{
var objectGuidClaim = context.Subject.Claims.FirstOrDefault(x => x.Type == "ObjectGUID");
if (objectGuidClaim != null)
{
var userId = new Guid(Convert.FromBase64String(objectGuidClaim.Value));
context.IssuedClaims.Add(new Claim("UserId", userId.ToString()));
}
return Task.CompletedTask;
}
public Task IsActiveAsync(IsActiveContext context)
{
context.IsActive = true;
return Task.CompletedTask;
}
}
因此,在我的情况下,如果没有 ProfileService,则通过 ObjectGUID,但使用 ProfileService,它在 context.Subject.Claims 列表中不可用。
我的目标是从作为 base64 编码的 guid 的 idp 中获取“ObjectGUID”声明,并将其转换为十六进制字符串,并将其作为来自身份服务器的“UserId”声明传递。
我什至不确定这是最好的方法。我也尝试通过ClaimActions 对其进行转换,但我的操作从未执行(我使用随机 guid 进行了测试以确保它与转换无关):
o.ClaimActions.MapCustomJson("UserId", obj => {
return Guid.NewGuid().ToString();
});
这是更好的方法吗?为什么不执行?
【问题讨论】:
-
请注意该服务可以被多个端点调用。您可以通过检查 context.caller 来确定端点。另请注意,应包含已配置的声明(使用 context.RequestedClaimTypes),然后应添加其他声明(claims.Add())。例如,请在此处查看我的答案:stackoverflow.com/questions/48006060/…
标签: c# asp.net-core-2.0 identityserver4 openid-connect claims-based-identity