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

Javascript 如何授予用户使用IdentityServer4访问特定API的权限?,javascript,asp.net,asp.net-core,identityserver4,openid-connect,Javascript,Asp.net,Asp.net Core,Identityserver4,Openid Connect,我正在构建一个新的安全令牌服务(在ASP.NET Core上运行IdentityServer4),需要让登录到前端应用程序的用户只访问某些API 到目前为止,我能够让用户登录到JS客户端,并通过访问特定JS客户端的令牌服务上配置为允许作用域的API获得授权/身份验证 我想做的是,将每个用户配置为只访问certian API,而不必访问允许范围内的所有API 我是新手,所以我甚至不确定我问的问题是否正确。但是,我希望这仍然是可以理解的,并且会愉快地回答任何问题 如何在令牌服务中定义我的用户: n

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

到目前为止,我能够让用户登录到JS客户端,并通过访问特定JS客户端的令牌服务上配置为允许作用域的API获得授权/身份验证

我想做的是,将每个用户配置为只访问certian 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
}
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"
};

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

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
}
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"
};

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

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

看看PolicyServer,他们对您遇到的问题有很好的介绍。

这就是用户授权,它不必在IdentityServer级别实现,实际上也是。他们的授权解决方案是。您还可以使用其他授权解决方案,如谢谢!但我发现,我可以设置每个用户对应用程序访问的同意。我使用IdentityServer4的GitHub中的快速入门示例发现了这一点。这难道不等于授予用户访问特定API资源的权限吗?客户端代表用户发出请求,但前提是用户已经同意(这是可配置的)。这意味着用户可以拒绝客户端对特定资源的访问。这将使用户处于控制状态。谢谢。正如我在上面所评论的,我刚刚发现,使用IdentityServer4的GitHub中的快速启动示例,我可以允许用户登录,并选择我希望同意的API资源。这不等于只允许用户访问某些API吗?这就是我第一句话的意思。:)用户允许应用程序访问他们的数据。因此,您可以让日历使用您的联系人。(API资源),或者您可以让您的日历访问您的生日(标识资源)。您不能做的是限制用户访问哪个API,即授权,而不是身份验证,这不属于IdentityServer 4的范围。同样,您可以通过分配给用户的索赔来实现这一点,但不建议这样做。哦,我明白了-我想我现在明白了。再次感谢!进一步调查PolicyServer,它开始有了明确的意义。这是一个艰难的过程。。很高兴我能帮忙。:)