【发布时间】:2019-05-30 15:24:01
【问题描述】:
我是 IdentityServer 的新手,我整天都在为这个问题苦苦挣扎。以至于我几乎要放弃这个了。我知道这个问题已经被问了一遍又一遍,我尝试了许多不同的解决方案,但似乎都没有奏效。希望你能帮助我把我推向正确的方向。
首先我通过运行dotnet new -i identityserver4.templates 安装了IdentityServer4 模板,并通过运行dotnet new is4aspid -o IdentityServer 创建了一个带有is4aspid 模板的新项目。
之后,我创建了一个新的 IdentityServer 数据库并运行了迁移。到那时我已经有了一个默认的身份数据库结构。
在 Config.cs 中,我将 MVC client 更改为以下内容:
new Client
{
ClientId = "mvc",
ClientName = "MVC Client",
AllowedGrantTypes = GrantTypes.Implicit,
ClientSecrets = { new Secret("47C2A9E1-6A76-3A19-F3C0-S37763QB36D9".Sha256()) },
RedirectUris = { "https://localhost:44307/signin-oidc" },
FrontChannelLogoutUri = "https://localhost:44307/signout-oidc",
PostLogoutRedirectUris = { "https://localhost:44307/signout-callback-oidc" },
AllowOfflineAccess = true,
AllowedScopes = { "openid", "profile", "api1", JwtClaimTypes.Role }
},
并将GetApis 方法更改为:
public static IEnumerable<ApiResource> GetApis()
{
return new ApiResource[]
{
new ApiResource("api1", "My API #1", new List<string>() { "role" })
};
}
当然,数据库中还没有用户,所以我添加了一个注册表单并注册了两个虚拟用户,一个是用户名admin@example.com,另一个是用户名subscriber@example.com。
为了将角色分配给这些用户,我在 Startup.cs 中创建了以下方法。
private async Task CreateUserRoles(IServiceProvider serviceProvider) {
var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
IdentityResult adminRoleResult;
IdentityResult subscriberRoleResult;
bool adminRoleExists = await RoleManager.RoleExistsAsync("Admin");
bool subscriberRoleExists = await RoleManager.RoleExistsAsync("Subscriber");
if (!adminRoleExists) {
adminRoleResult = await RoleManager.CreateAsync(new IdentityRole("Admin"));
}
if(!subscriberRoleExists) {
subscriberRoleResult = await RoleManager.CreateAsync(new IdentityRole("Subscriber"));
}
ApplicationUser userToMakeAdmin = await UserManager.FindByNameAsync("admin@example.com");
await UserManager.AddToRoleAsync(userToMakeAdmin, "Admin");
ApplicationUser userToMakeSubscriber = await UserManager.FindByNameAsync("subscriber@example.com");
await UserManager.AddToRoleAsync(userToMakeSubscriber, "Subscriber");
}
在同一个类的Configure方法中,我添加了参数IServiceProvider services,并像这样调用上面的方法:CreateUserRoles(services).Wait();。此时我的数据库中确实有两个角色。
接下来,我创建了一个新的解决方案(在同一个项目中),并在该解决方案的 Startup.cs 文件中,我在 ConfigureServices 方法中添加了以下内容。
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options => {
options.SaveTokens = true;
options.ClientId = "mvc";
options.ClientSecret = "32D7A7W0-0ALN-2Q44-A1H4-A37990NN83BP";
options.RequireHttpsMetadata = false;
options.Authority = "http://localhost:5000/";
options.ClaimActions.MapJsonKey("role", "role");
});
之后我在同一个类的Configure方法中添加了app.UseAuthentication();。
然后我使用以下 if 语句创建了一个新页面。
if(User.Identity.IsAuthenticated) {
<div>Yes, user is authenticated</div>
}
if(User.IsInRole("ADMIN")) {
<div>Yes, user is admin</div>
}
我使用 admin@example.com 登录,但第二个 if 语句返回 False。我通过像这样遍历它们来检查所有声明。
@foreach (var claim in User.Claims) {
<dt>@claim.Type</dt>
<dd>@claim.Value</dd>
}
但是没有找到角色声明,只有 sid、sub、idp、preferred_username 和 name。
我试图在那里获得角色,以便第二个 if 语句返回 True,但在尝试和尝试之后,我还没有能够使其工作。有人可以看到我必须做什么才能完成这项工作吗?我是 IdentityServer4 的绝对初学者,并尽我所能理解它。任何帮助将不胜感激。提前致谢!
编辑 1:
感谢this question 和this question,我感觉自己走在了正确的轨道上。我做了一些修改,但我仍然无法让它工作。我刚刚尝试了以下方法。
- 在我的 IdentityServer 项目中创建了一个新的 ProfileService 类,其内容如下。
public class MyProfileService : IProfileService {
public MyProfileService() { }
public Task GetProfileDataAsync(ProfileDataRequestContext context) {
var roleClaims = context.Subject.FindAll(JwtClaimTypes.Role);
List<string> list = context.RequestedClaimTypes.ToList();
context.IssuedClaims.AddRange(roleClaims);
return Task.CompletedTask;
}
public Task IsActiveAsync(IsActiveContext context) {
return Task.CompletedTask;
}
}
接下来我通过添加services.AddTransient<IProfileService, MyProfileService>(); 行在ConfigureServices 方法中注册了这个类。之后,我在 GetIdentityResources 方法中添加了一个新行,现在看起来像这样。
public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new IdentityResource[]
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResource("roles", new[] { "role" })
};
}
我还将角色添加到我的 Mvc 客户端,如下所示:AllowedScopes = { "openid", "profile", "api1", "roles" }。
接下来我切换到另一个项目并在 .AddOpenIdConnect oidc 中添加了以下几行。
options.ClaimActions.MapJsonKey("role", "role", "role");
options.TokenValidationParameters.RoleClaimType = "role";
但是,我仍然无法让它像我想要的那样工作。有人知道我错过了什么吗?
【问题讨论】:
-
搜索这个确切的问题,我已经在这里回答了,只是现在无法在移动设备上查看。
-
感谢您的帮助,我确实看到了您的回答并尝试实施您的解决方案。但是,第二个 if 语句仍然返回 False,并且该角色不是声明的一部分。我已经用我添加的代码编辑了我的帖子。如果您能向我解释我做错了什么,那将非常有帮助。提前致谢!
-
到目前为止看起来不错,我会大胆猜测并说它可能区分大小写,所以试试
IsInRole("Admin")。还可以使用 jwt.io 之类的东西检查原始令牌,并检查role声明现在是否已正确添加到令牌中。此外,您还没有按照我的回答实现您的ProfileService。 -
@Mitch,在您的客户端配置中看不到
options.ResponseType = "id_token token";,因此您只请求IdentityResiource信息而不是ApiResource。尽管如此,在您编辑之后,您应该能够使用 id_token 获得您的角色,但您必须明确提出要求:options.Scope.Add("roles"); -
不幸的是,他们都不是这样。无论我将 isInRole("ADMIN") 更改为 isInRole("Admin") 还是更改为 isInRole("admin"),它们都返回 False。解码的 JWT 令牌也不包含角色密钥。但是,如果我添加 options.Scope.Add("roles");到客户端它返回角色属性并且 isInRole("Admin") 返回真!感谢您的建议和帮助!它现在应该像现在一样工作:)
标签: c# .net-core identityserver4