我有一个带有 Keycloak 的 docker 容器。 我正在尝试了解身份验证的工作原理,特别是在声明映射方面。
我创建了一个领域,并在
Realm Settings > User Profile
中创建了一个新的自定义属性
通过这个简单的配置,从我的 AspNet Core Razor 应用程序登录后,我能够收到令牌中的声明
phone_number
(access_token、id_token,具体取决于我的配置)。 phoneNumber
属性也出现在注册表中,这很好。
{
"exp": 1726218720,
"iat": 1726218420,
"auth_time": 1726218420,
"jti": "86a79a1a-ccd2-478b-9b98-6d67e94207a6",
"iss": "http://localhost:9001/realms/web-portal",
"aud": "account",
"sub": "d7af94a3-a675-4dd1-b58f-8d2baa29c1f1",
"typ": "Bearer",
"azp": "web-portal-client",
"sid": "8391d1be-584f-4f3c-91c4-55979a57e635",
"acr": "1",
"allowed-origins": [
"http://localhost:9999"
],
"realm_access": {
"roles": [
"offline_access",
"default-roles-web portal",
"uma_authorization"
]
},
"resource_access": {
"account": {
"roles": [
"manage-account",
"manage-account-links",
"view-profile"
]
}
},
"scope": "openid profile offline_access phone email",
"email_verified": true,
"name": "Tenant Admin",
"phone_number": "123456789",
"preferred_username": "[email protected]",
"given_name": "Tenant",
"family_name": "Admin",
"email": "[email protected]"
}
但是,因为我的客户要求标准
phone
范围
builder.Services
.AddAuthentication(options => {
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options => {
// omitted for brevity
})
.AddOpenIdConnect(options => {
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.Authority = $"{tokenConfiguration.Authority}/realms/{tokenConfiguration.Realm}";
options.RequireHttpsMetadata = tokenConfiguration.RequireHttpsMetadata;
options.ClientId = tokenConfiguration.ClientId;
options.ClientSecret = tokenConfiguration.ClientSecret;
options.ResponseType = OpenIdConnectResponseType.Code;
options.UsePkce = true;
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
options.Scope.Add("phone");
options.Scope.Add("offline_access");
options.MapInboundClaims = false;
options.SaveTokens = true;
options.ClaimActions.MapJsonKey("email_verified", "email_verified");
options.ClaimActions.MapJsonKey("phone_verified", "phone_verified");
options.GetClaimsFromUserInfoEndpoint = true;
options.TokenValidationParameters = new TokenValidationParameters {
NameClaimType = "name",
RoleClaimType = "roles"
};
});
我预计会自动收到两个标准索赔:
phone_number
phone_number_verified
这是正确的吗?我是 keycloak 的新手,主要是尝试将我所掌握的知识与其他 IdP 相匹配。我也检查了这个问题,它非常相似,但没有答案。