覆盖默认的 Identity Server 4 客户端
Override default Identity Server 4 Client
我创建了一个 .NET Core 3.0 Angular project with Identity Server。我想向我的应用程序添加声明和角色。
我的 Identity Server 大部分都是开箱即用的,只是进行了一些简单的路由更改:
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityServer(options =>
{
options.UserInteraction.LoginUrl = "/auth/login";
options.UserInteraction.LogoutUrl = "/auth/logout";
})
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
我目前在 startup.cs
中添加了我的简单政策
services.AddAuthorization(options =>
{
options.AddPolicy("RequireAdministratorRole", policy =>
{
policy.RequireRole("Admin");
policy.RequireClaim("CreateUser");
});
});
在我的 Configure()
方法的底部,我调用此方法来添加角色:
private async Task CreateUserRoles(IServiceProvider serviceProvider)
{
var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
IdentityResult adminRoleResult;
//Adding Admin Role
var adminRoleCheck = await RoleManager.RoleExistsAsync("Admin");
if (!adminRoleCheck)
{
//create the roles and seed them to the database
adminRoleResult = await RoleManager.CreateAsync(new IdentityRole("Admin"));
await RoleManager.AddClaimAsync(new IdentityRole("Admin"), new Claim("CreateUser", "Create User"));
}
}
在我的 register.cshtml.cs
中,我已通过以下方式成功设置角色和声明:
var roleResult = await _userManager.AddToRoleAsync(user, "Admin");
var claimResult = await _userManager.AddClaimAsync(user, new Claim("CreateUser", "Create User"));
我已确认新用户具有该声明和角色。
客户端 userinfo
调用 returns 正确,但是当我查看 id_token
时,我没有这些声明:
{
"nbf": 1574787988,
"exp": 1574788288,
"iss": "https://localhost:5001",
"aud": "MyAppSpa",
"iat": 1574787988,
"at_hash": "ndzldxAE3EiVzI4PeThNPQ",
"s_hash": "dIqJXx372XhOESn1XYH36A",
"sid": "VQLp--MHdoOoxXiVASWZ0g",
"sub": "4a0450dd-fe4f-4b3d-ac12-3f70876183e1",
"auth_time": 1574787983,
"idp": "local",
"amr": [
"pwd"
]
}
根据 ,我应该只需要在客户端配置中包含 AlwaysIncludeUserClaimsInIdToken = true
。
但是,模板没有配置。一切都在幕后。
问题:
1) 将 AlwaysIncludeUserClaimsInIdToken = true
添加到客户端是否可以解决我的问题?
2) 根据我当前的配置如何添加它?
关注 ID Token 的大小,默认情况下声明不会包含在 ID token 中。您可以使用 ID token 从 userinfo 端点获取声明(从 user.profile
读取)。那是设计使然。
新的 .net core 3.0 angular 身份验证模板只是将 IdentityServer 配置为使用模板支持的配置,但与 Identity Server 提供的配置相比,配置并不完全自定义,例如 AlwaysIncludeUserClaimsInIdToken
。因此解决方法是不使用 ApiAuthorization
服务,IdentityServer 的全部功能仍然可用于自定义身份验证以满足您的需要。
我创建了一个 .NET Core 3.0 Angular project with Identity Server。我想向我的应用程序添加声明和角色。
我的 Identity Server 大部分都是开箱即用的,只是进行了一些简单的路由更改:
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityServer(options =>
{
options.UserInteraction.LoginUrl = "/auth/login";
options.UserInteraction.LogoutUrl = "/auth/logout";
})
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
我目前在 startup.cs
services.AddAuthorization(options =>
{
options.AddPolicy("RequireAdministratorRole", policy =>
{
policy.RequireRole("Admin");
policy.RequireClaim("CreateUser");
});
});
在我的 Configure()
方法的底部,我调用此方法来添加角色:
private async Task CreateUserRoles(IServiceProvider serviceProvider)
{
var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
IdentityResult adminRoleResult;
//Adding Admin Role
var adminRoleCheck = await RoleManager.RoleExistsAsync("Admin");
if (!adminRoleCheck)
{
//create the roles and seed them to the database
adminRoleResult = await RoleManager.CreateAsync(new IdentityRole("Admin"));
await RoleManager.AddClaimAsync(new IdentityRole("Admin"), new Claim("CreateUser", "Create User"));
}
}
在我的 register.cshtml.cs
中,我已通过以下方式成功设置角色和声明:
var roleResult = await _userManager.AddToRoleAsync(user, "Admin");
var claimResult = await _userManager.AddClaimAsync(user, new Claim("CreateUser", "Create User"));
我已确认新用户具有该声明和角色。
客户端 userinfo
调用 returns 正确,但是当我查看 id_token
时,我没有这些声明:
{
"nbf": 1574787988,
"exp": 1574788288,
"iss": "https://localhost:5001",
"aud": "MyAppSpa",
"iat": 1574787988,
"at_hash": "ndzldxAE3EiVzI4PeThNPQ",
"s_hash": "dIqJXx372XhOESn1XYH36A",
"sid": "VQLp--MHdoOoxXiVASWZ0g",
"sub": "4a0450dd-fe4f-4b3d-ac12-3f70876183e1",
"auth_time": 1574787983,
"idp": "local",
"amr": [
"pwd"
]
}
根据 AlwaysIncludeUserClaimsInIdToken = true
。
但是,模板没有配置。一切都在幕后。
问题:
1) 将 AlwaysIncludeUserClaimsInIdToken = true
添加到客户端是否可以解决我的问题?
2) 根据我当前的配置如何添加它?
关注 ID Token 的大小,默认情况下声明不会包含在 ID token 中。您可以使用 ID token 从 userinfo 端点获取声明(从 user.profile
读取)。那是设计使然。
新的 .net core 3.0 angular 身份验证模板只是将 IdentityServer 配置为使用模板支持的配置,但与 Identity Server 提供的配置相比,配置并不完全自定义,例如 AlwaysIncludeUserClaimsInIdToken
。因此解决方法是不使用 ApiAuthorization
服务,IdentityServer 的全部功能仍然可用于自定义身份验证以满足您的需要。