IdentityServer4 - ApiResource和Client,它们是如何捆绑在一起的

blg*_*boy 6 c# asp.net-core identityserver4

我试图确定ApiResource和Client是如何联系在一起的.

如何确保从客户端请求令牌的人请求特定的ApiResource可以访问该ApiResource?

是否被Scopes捆绑在一起?

以下是QuickStart中稍微修改过的代码:

public static IEnumerable<ApiResource> GetApiResources()
{
    return new List<ApiResource>
    {
        new ApiResource("api1Resource", "My API")
        {
            Scopes = 
            {
                new Scope("api1"),
                new Scope("api1.ro"),
                new Scope("offline_access")
            },
            UserClaims = { "role", "user" }
        }
    };
}

// client want to access resources (aka scopes)
public static IEnumerable<Client> GetClients()
{
    // client credentials client, for APIs
    return new List<Client>
    {
        new Client
        {
            ClientId = "apiClient",
            AllowedGrantTypes = GrantTypes.ClientCredentials,

            ClientSecrets =
            {
                // Secret that can be created and given to ITSM_API
                new Secret("secret".Sha512(), "ITSM_API Secret")
            },
            AllowedScopes = { "api1", "api1.ro", "offline_access" }
        },

        // resource owner password grant client, for interactive users
        new Client
        {
            ClientId = "userClient",
            AllowedGrantTypes = GrantTypes.List
            (
                GrantType.ResourceOwnerPassword,
                "offline_access"
            ),
            ClientSecrets = 
            {
                new Secret("secret".Sha512(), "userClient Secret")
            },
            UpdateAccessTokenClaimsOnRefresh = true,
            AllowedScopes = { "api1", "api1.ro", "offline_access" },
            AbsoluteRefreshTokenLifetime = 86400,
            AllowOfflineAccess = true,
            RefreshTokenUsage = TokenUsage.ReUse
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

mor*_*tzg 0

范围是您通过资源服务器提供的资源。例如,如果您有一个日历资源服务器,您的范围将是calendarentry, read.calendarentry, create.calendarentry。基本上,您的用户可以在您的服务器上执行一些操作。

API 资源是您的整个资源服务器。客户端(获得 access_token 的客户端)请求其所需的范围,然后用户向客户端授予权限。

范围被放入access_token中,当资源服务器收到access_token时,您需要检查是否允许用户(由access_token标识)访问所请求的范围。(这可以预先在 IdentityServer 上完成)。例如,您可以在登录时检查用户数据库是否有权访问您定义的 API 资源。IdentityServer 的可配置性非常好,几乎可以满足所有设置。