Asp.net core mvc 覆盖默认的Identity Server 4客户端
我创建了一个。我想向我的应用程序添加声明和角色 我的Identity Server大多是开箱即用的,只需进行一些简单的路由更改:Asp.net core mvc 覆盖默认的Identity Server 4客户端,asp.net-core-mvc,identityserver4,openid-connect,Asp.net Core Mvc,Identityserver4,Openid Connect,我创建了一个。我想向我的应用程序添加声明和角色 我的Identity Server大多是开箱即用的,只需进行一些简单的路由更改: services.AddIdentity<ApplicationUser, IdentityRole>() .AddDefaultTokenProviders() .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityServer(options =>
{
options.UserInteraction.LoginUrl = "/auth/login";
options.UserInteraction.LogoutUrl = "/auth/logout";
})
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
在我的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"));
}
}
我已经确认新用户拥有该声明和角色
客户端userinfo
调用返回正确,但当我查看id\u令牌时,我没有这些声明:
{
"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令牌的大小,默认情况下,声明不会包含在ID令牌中。您可以使用ID令牌从userinfo端点获取声明(从user.profile
读取)。这是故意的
新的.net core 3.0 angular身份验证模板仅将IdentityServer配置为使用模板支持的配置,但与Identity server提供的配置(如AlwaysIncludeUserClaimsInIdToken
)相比,该配置不是完全自定义的。因此,解决方法不是使用ApiAuthorization
服务,IdentityServer的全部功能仍然可用于定制身份验证以满足您的需要。初始id_令牌仅包含子声明(按设计)。可以使用访问令牌在UserInfo端点检索其他信息。但问题是,您不需要id_令牌中的声明,而需要访问令牌中的声明。因此,与其添加IdentityClaims,不如添加ApiClaims。请阅读我的答案以获得更多解释。我开始得出相同的结论,我的主要问题是使用ApiAuthorization
,让我做一切。我想我得仔细考虑一下。您是否知道他们在中定义客户机
对象的位置?我想从他们使用的确切配置开始,并从那里调整它。
var roleResult = await _userManager.AddToRoleAsync(user, "Admin");
var claimResult = await _userManager.AddClaimAsync(user, new Claim("CreateUser", "Create User"));
{
"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"
]
}