首页 > 解决方案 > IdentityServer4 ClientCredentials 基本问题(我认为)

问题描述

这可能是关于 IdentityServer4 的一个基本问题

所以我通过identityserver4 docs工作,我已经完成了

[使用客户端凭据保护 API] https://docs.identityserver.io/en/latest/quickstarts/1_client_credentials.html#

我明白了,所以我设置了一个带有 Api 范围(api1)的 APi 资源,我的“客户端”使用客户端凭据和该范围

public static IEnumerable<Client> Clients =>
    new List<Client>
    {
        new Client
        {
            ClientId = "client",

            // no interactive user, use the clientid/secret for authentication
            AllowedGrantTypes = GrantTypes.ClientCredentials,

            // secret for authentication
            ClientSecrets =
            {
                new Secret("secret".Sha256())
            },

            // scopes that client has access to
            AllowedScopes = { "api1" }
        }
    };

好的,然后我执行下一部分“使用 ASP.NET Core 的交互式应用程序”,我明白了

所以我的客户需要很高兴地做到这两点,下一节“ASP.NET Core 和 API 访问” - 将它们结合在一起,这说明我所要做的就是

new Client
{
    ClientId = "mvc",
    ClientSecrets = { new Secret("secret".Sha256()) },

    AllowedGrantTypes = GrantTypes.Code,

    // where to redirect to after login
    RedirectUris = { "https://localhost:5002/signin-oidc" },

    // where to redirect to after logout
    PostLogoutRedirectUris = { "https://localhost:5002/signout-callback-oidc" },

    AllowOfflineAccess = true,

    AllowedScopes = new List<string>
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        "api1"
    }
}

哪个有效

我不明白的是 - IdentityServer4 客户端“客户端”具有

AllowedGrantTypes = GrantTypes.ClientCredentials

那么“mvc”客户端如何只需将“api1”添加到允许的范围即可获取客户端凭据?

我在文档中看不到解释

是约定“客户端凭据”的不记名令牌(所以我不再需要客户端“客户端”)?或者 IdentityServer4 是否基于它们都具有 api1 范围的事实以某种方式链接客户端?

谢谢

标签: c#asp.net-mvcidentityserver4

解决方案


ClientCredentials,用于服务到服务的通信。也许您有一个需要与 API 对话的后台工作。让一个 API 与另一个 API 对话。

授权代码流通常适用于 MVC 应用程序,在该应用程序中,您有一个用户登录到该站点,然后您会返回一个访问令牌,然后您可以将其发送到给定的 API,例如本例中的 API1。

在您的场景中,您实际上并不需要同时使用两者。对于以 api1 为范围的面向公众的服务器,仅授权代码流就足够了。

当您与 API 交谈时,您在两个流程中都使用不记名令牌(授权标头)。


推荐阅读