【发布时间】:2021-04-18 18:12:07
【问题描述】:
我目前正在开发一个 .net5.0 IdentityServer4 应用程序。
我想使用基于角色的授权,不幸的是它似乎无法正常工作。
当我在控制器操作上使用[Authorization] 属性时,我可以正确登录。
当我在控制器操作中使用 [Authorize(Roles = "admin")] 属性时,我会被转发到拒绝访问页面 (403)。
当我使用我的 TestUser 和角色 admin 登录时,我会被转发到我的客户端应用程序上的拒绝访问页面。
我的应用程序结构如下所示:
- IdentityServer4
- OIDC客户端(文App,授权码+PKCE)
IdentityServer4 配置如下所示:
public static IEnumerable<Client> GetClients()
{
return new List<Client>
{
new Client
{
ClientId = "oidcClient",
ClientName = "Example Client Application",
ClientSecrets = new List<Secret> { new Secret("secret".Sha256()) },
AllowedGrantTypes = GrantTypes.Code,
RedirectUris = new List<string> { "https://localhost:xxxx/signin-oidc" },
AllowedScopes = new List<string>
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
"role"
},
RequirePkce = true,
AllowPlainTextPkce = false
}
};
public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new[]
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResources.Email(),
new IdentityResource
{
Name = "role",
UserClaims = new List<string> { "role" }
}
};
}
public static List<TestUser> GetUsers()
{
return new List<TestUser> {
new TestUser {
SubjectId = "123ABC",
Username = "admin",
Password = "password",
Claims = new List<Claim> {
new Claim(ClaimTypes.Email, "admin@test.com"),
new Claim(ClaimTypes.Role, "admin")
}
}
};
}
我的客户端应用上的 Oidc 连接配置如下所示:
services.AddAuthentication(options =>
{
options.DefaultScheme = "cookie";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("cookie")
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "https://localhost:5000";
options.ClientId = "oidcClient";
options.ClientSecret = "secret";
options.ResponseType = "code";
options.UsePkce = true;
options.ResponseMode = "query";
// Edit 1
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = ClaimTypes.Name,
RoleClaimType = ClaimTypes.Role
};
options.SaveTokens = true;
});
我的 controller 操作非常简单 - 只有角色还没有策略 - 看起来像这样:
[Authorize(Roles = "admin")] // Roles does not work
//[Authorize] works fine
public IActionResult Home()
{
return View();
}
感觉好像访问令牌没有传递给我的客户——只有身份令牌......
我使用 IdentityServer4 TestUser,但我还没有 IProfileService 实现。有必要吗?
你知道如何解决这个问题吗?
如何在我的应用程序中正确使用角色声明?
【问题讨论】:
-
您能出示您的授权政策吗?
-
感谢您的评论!我还没有使用策略——它是一个简单的 OIDC 客户端启动器。我只在控制器操作中使用 [Authorize(Roles = "admin")] 属性。
标签: c# identityserver4 openid-connect roles claims