【问题标题】:IdentityServer3 PostMan invalid_clientIdentityServer3 PostMan invalid_client
【发布时间】:2018-07-14 15:17:33
【问题描述】:

我已经设置了一个在 IIS 中运行的 IdentityServer3 实例。

        var validators = new List<Registration<ISecretValidator>>
        {
            new Registration<ISecretValidator, HashedSharedSecretValidator>(),
            new Registration<ISecretValidator, X509CertificateThumbprintSecretValidator>()
        };

        // .Register() is an extension method that setups that setups the
        // IdentityServerServiceFactory
        var factory = new EntityFrameworkServiceOptions()
                    .Register()
                    .UseInMemoryUsers(Users.Get());
        factory.SecretValidators = validators;

        app.Map($"/{IdentityServer.Path}", server =>
        {
            server.UseIdentityServer(new IdentityServerOptions()
            {
                RequireSsl = false,
                SiteName = siteName,
                SigningCertificate = Certificate.Load(),
                Factory = factory,

                // Currently does nothing. There are no plugins.
                PluginConfiguration = ConfigurePlugins,
                AuthenticationOptions = new AuthenticationOptions()
                {
                    EnablePostSignOutAutoRedirect = true,

                    // Currently does nothing. There are no IdentityProviders setup
                    IdentityProviders = ConfigureIdentityProviders
                }
            });
        });

我在 EF 数据库中为客户端凭据流设置了一个客户端。所以Client 表中有一个客户端,我已授予客户端对ClientScopes 表中范围的访问权限,并在ClientSecrets 表中为客户端提供了一个秘密。

存储在数据库中的相关值是(所有未列出的值都是 IdentityServer3 默认值):

ClientId = 'client'
Flow = 'ClientCredentials [3]'
ClientScope = 'api'
ClientSecret = 'secret'.Sha256()

我正在尝试从 Postman 获取新令牌:

IdentityServer 正在测试服务器上运行,这就是我没有选择“本地请求访问令牌”的原因。

当我点击“请求令牌”时,我收到以下错误记录:

2016-09-16 16:18:28.470 -05:00 [Debug] Start client validation
2016-09-16 16:18:28.470 -05:00 [Debug] Start parsing Basic Authentication secret
2016-09-16 16:18:28.470 -05:00 [Debug] Parser found secret: "BasicAuthenticationSecretParser"
w3wp.exe Information: 0 : 2016-09-16 16:18:28.470 -05:00 [Information] Secret id found: "client"
2016-09-16 16:18:28.470 -05:00 [Debug] No matching hashed secret found.
w3wp.exe Information: 0 : 2016-09-16 16:18:28.470 -05:00 [Information] Secret validators could not validate secret
w3wp.exe Information: 0 : 2016-09-16 16:18:28.470 -05:00 [Information] Client validation failed.
w3wp.exe Information: 0 : 2016-09-16 16:18:28.470 -05:00 [Information] End token request
w3wp.exe Information: 0 : 2016-09-16 16:18:28.470 -05:00 [Information] Returning error: invalid_client

我不太确定为什么验证器无法验证秘密。它以 Sha256 的形式保存在数据库中,IdentityServer 可以解析和验证 Sha256。

更新: 我让它工作从邮递员做一个 POST 并填写适当的 x-www-form-urlencoded 字段,但我仍然没有弄清楚如何使用授权选项卡和“获取新访问令牌”功能让它工作邮递员。不能用来从 IdentityServer3 获取访问令牌吗?

【问题讨论】:

    标签: c# oauth-2.0 identityserver3


    【解决方案1】:

    对 Postman 中内置的 OAuth2 令牌的支持运行良好。您可以使用任一客户端。客户端凭据和授权代码授权类型都可以正常工作 - 如果您将授权代码类型设置为如下所示,您甚至会得到一个弹出窗口,允许您输入用户名和密码。这是我用于授权代码流的客户端条目:

    new Client
    {
        ClientId = "postmantestclient",
        ClientName = "Postman http test client",
        Flow = Flows.AuthorizationCode,
        AllowAccessToAllScopes = true,
        IdentityTokenLifetime = 60 * 60 * 24,
        AccessTokenLifetime = 60 * 60 * 24,
        RequireConsent = false,
        ClientSecrets = new List<Secret>
        {
            new Secret("PostmanSecret".Sha256())
        },
        RedirectUris = new List<string>()
        {
            "https://www.getpostman.com/oauth2/callback"
        }
    }
    

    这是我设置邮递员请求的方式

    不是对话框中的 URL。系统不是很宽容,如果您输入错误的 URL,您在发出请求时很可能会看到完全虚假的 CORS 错误。

    【讨论】:

    • 我将回调 url 添加到客户端的重定向 uri。我将我的令牌名称更新为“Bearer”。我将 Auth url 更改为授权端点。 (我的授权类型仍然是客户凭证)。我在本地检查了请求访问令牌复选框,但我仍然使用这种方法得到“invalid_client”......客户端是否必须使用“授权码”流程进行设置?
    • 我已经使用客户端凭据和资源代码让它工作了。唯一的区别是资源代码 Postman 会弹出一个登录对话框。这对于我正在工作的开发来说很方便,因为许多 REST 服务调用需要与特定用户相关联。
    【解决方案2】:

    我已经让它工作了,但没有使用 Postman 的“获取新访问令牌”功能。我不知道为什么这不起作用:p 相反,我只是发布到令牌 URL,它给了我一个访问令牌,然后我可以在后续调用我的服务时使用它。

    POST: https://{{server}}/connect/token
    client_id:
    client_secret:
    grant_type: client_credentials
    scope:
    

    然后在您的服务器调用中使用它,将以下内容添加到您的标题中:

    授权:承载 [access_token]

    【讨论】:

      猜你喜欢
      • 2017-10-02
      • 2021-01-22
      • 2017-05-16
      • 1970-01-01
      • 2019-08-31
      • 2023-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多