gpt4 book ai didi

javascript - 如何使用 IdentityServer4 授予用户对特定 API 的访问权限?

转载 作者:行者123 更新时间:2023-12-02 22:43:57 25 4
gpt4 key购买 nike

我正在构建一个新的安全 token 服务(在 ASP.NET Core 上运行 IdentityServer4),并且需要让登录到前端应用程序的用户仅访问某些 API。

到目前为止,我可以让用户登录到 JS 客户端,并获得授权/身份验证,可以访问配置为该特定 JS 客户端的 token 服务上允许的范围的 API。

我想要做的是将每个用户配置为只能访问某些 API - 不一定是允许范围内的所有 API。

我是新手,所以我什至不确定我是否问了正确的问题。但是,我希望它仍然可以理解,并且很乐意回答任何问题。

<小时/>

如何在 token 服务中定义我的用户:

new TestUser
{
SubjectId = "05624",
Username = "asd",
Password = "asd",
Claims = new []
{
new Claim("name", "My Name"),
new Claim("email", "myname@email.net")
}
}
<小时/>

如何在 token 服务中定义我的客户端:

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
}
<小时/>

如何在 token 服务中定义 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 无意这样做。它授予应用程序使用用户身份的权限,而不是用户使用应用程序的权限。

如果您只是构建一个简单的系统,您可以分配用户 Angular 色声明,并在您的应用程序中制定策略,根据用户的 Angular 色允许/禁止用户。但随着您的权限逻辑不断增长,这将变得难以管理。

看看PolicyServer,他们对您遇到的问题有一个很好的演示。 https://policyserver.io/

关于javascript - 如何使用 IdentityServer4 授予用户对特定 API 的访问权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58484046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com