【问题标题】:IdentityServer4 - ApiResource and Client, how are they tied togetherIdentityServer4 - ApiResource 和 Client,它们是如何联系在一起的
【发布时间】:2017-05-23 22:01:44
【问题描述】:

我正在尝试确定 ApiResource 和 Client 是如何联系在一起的。

我如何确保从客户端请求令牌的人正在为特定的 ApiResource 请求令牌,该 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
        }
    };
}

【问题讨论】:

  • "offline_access" 不是有效的授权类型。

标签: c# asp.net-core identityserver4


【解决方案1】:

阅读本文可能会有所帮助...https://leastprivilege.com/2016/12/01/new-in-identityserver4-resource-based-configuration/。在此之前,没有资源,只有范围。范围的抽象性质意味着事情并不总是显而易见的,因此发明了资源。

因此,您当前指定Client &gt; AllowedScopes 的位置可以只引用您的资源,而不是重复您在资源中定义的范围。 https://identityserver4.readthedocs.io/en/release/reference/api_resource.html

【讨论】:

    【解决方案2】:

    范围是您通过资源服务器提供的资源。例如,如果您有一个日历资源服务器,您的范围将是 calendarentryread.calendarentrycreate.calendarentry。所以基本上你的用户可以在你的服务器上做的事情。

    API 资源是您的整体资源服务器。客户端(获取 access_token 的那个),请求它需要的范围,用户将权限授予客户端。

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

    【讨论】:

      猜你喜欢
      • 2019-01-07
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 2020-02-08
      相关资源
      最近更新 更多