【发布时间】:2020-02-17 09:50:42
【问题描述】:
我正在构建一个新的安全令牌服务(在 ASP.NET Core 上运行 IdentityServer4),并且需要让登录到前端应用程序的用户只能访问某些 API。
到目前为止,我能够让用户登录到 JS 客户端,并通过访问配置为特定 JS 客户端令牌服务上允许范围的 API 的权限获得授权/身份验证。
我希望做的是将每个用户配置为只能访问某些 API - 不一定是允许范围内的所有 API。
我是新手,所以我什至不确定我问的问题是否正确。但是,我希望它仍然可以理解,并且很乐意回答任何问题。
我如何在令牌服务中定义我的用户:
new TestUser
{
SubjectId = "05624",
Username = "asd",
Password = "asd",
Claims = new []
{
new Claim("name", "My Name"),
new Claim("email", "myname@email.net")
}
}
我如何在令牌服务中定义我的客户端:
new Client
{
ClientId = "js-demo"
AllowedGrantTypes = GrantTypes.Code,
AllowedScopes =
{
"openid",
"api_1",
"api_2"
},
AllowedCorsOrigins = { "http://localhost:5000" },
RedirectUris = { "http://localhost:5000/callback.html" },
PostLogoutRedirectUris = { "http://localhost:5000/index.html" },
RequirePkce = true,
RequireClientSecret = false,
AllowAccessTokensViaBrowser = true
}
我如何在令牌服务中定义我的 API 资源:
new List<ApiResource>
{
new ApiResource("api_1"),
new ApiResource("api_2")
};
我如何定义我的前端客户端:
var config = {
client_id: "js-demo",
response_type: "code",
scope: "openid api_1 api_2",
authority: "http://localhost:9999",
redirect_uri: "http://localhost:5000/callback.html",
post_logout_redirect_uri: "http://localhost:5000/index.html"
};
【问题讨论】:
-
那是用户授权,不一定要在IdentityServer级别实现,实际上也是not recommended there。他们的授权解决方案是PolicyServer。您还可以使用其他授权解决方案,例如 resource-based authorization
-
谢谢!我发现的一件事是,我可以设置每个用户对应用程序访问的同意。我使用 IdentityServer4 的 GitHub 中的快速入门示例找到了这一点。这与授予用户访问特定 API 资源的权限不同吗?
-
客户端代表用户提出请求,但前提是用户同意(这是可配置的)。这意味着用户可以拒绝客户端访问某个资源。这让用户可以控制。
标签: javascript asp.net asp.net-core identityserver4 openid-connect