【问题标题】:Identity Server OAuth Resource Owner Password Grant always returns invalid_client身份服务器 OAuth 资源所有者密码授予始终返回 invalid_client
【发布时间】:2015-09-08 05:31:09
【问题描述】:
new Client
{
    ClientId = "esmifavorito",
    ClientName = "esmifavorito-client",
    Enabled = true,
    ClientSecrets = new List<ClientSecret>
    {
        new ClientSecret("esmifavorito".Sha256()) //PQ/pIgjXnBfK67kOxGxz9Eykft6CKPkPewR3jUNEkZo=
    },

    Flow = Flows.ResourceOwner,

    //RequireConsent = false,
    //AllowRememberConsent = false,
    //ClientUri = "http",
    RedirectUris = new List<string>
    {
        "https://localhost:44304",
    },

    ScopeRestrictions = new List<string>
    {
    },

    AllowedCorsOrigins = new List<string> 
    {
        "https://localhost:44304",
        "http://localhost:50655",
        "chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm",
        "*",
    },

    PostLogoutRedirectUris = new List<string>
    {
        "https://localhost:44304",
    },

    AccessTokenType = AccessTokenType.Jwt,
    IdentityTokenLifetime = 3000,
    AccessTokenLifetime = 3600,
    AuthorizationCodeLifetime = 300
}

我已经注册了我的客户,它可以通过隐式流程工作,但我需要实现一个登录表单,所以我正在尝试授予资源所有者密码凭据。 我正在使用 Chrome 中的 Postman 向端点发出请求(这就是为什么我将 chrome-extension 添加到 CORS,只是为了看看这是否是错误......)

我尝试了很多请求(使用 https)

POST /connect/token HTTP/1.1
Host: localhost:44302
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded

grant_type=password&username=test&password=testuser&client_id=esmifavorito

-

POST /connect/token HTTP/1.1
Host: localhost:44302
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded

grant_type=password&username=test&password=testuser&client_id=esmifavorito&client_secret=PQ%2FpIgjXnBfK67kOxGxz9Eykft6CKPkPewR3jUNEkZo%3D

-

POST /connect/token HTTP/1.1
Host: localhost:44302
Authorization: Basic ZXNtaWZhdm9yaXRvOlBRL3BJZ2pYbkJmSzY3a094R3h6OUV5a2Z0NkNLUGtQZXdSM2pVTkVrWm89
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded

grant_type=password&username=test&password=testuser

那些应该有效,但我总是得到 invalid_client

错误日志是空的,不知道是否做了tracer注册对

LogProvider.SetCurrentLogProvider(new DiagnosticsTraceLogProvider());
app.UseIdentityServer(new IdentityServerOptions
    {
        LoggingOptions = new LoggingOptions {
            IncludeSensitiveDataInLogs = true,
            WebApiDiagnosticsIsVerbose = true,
            EnableWebApiDiagnostics = true,
            //EnableHttpLogging = true
        },
        SiteName = "Thinktecture IdentityServer3 - UserService-AspNetIdentity",
        SigningCertificate = Certificate.Get(string.Format(@"{0}\bin\IdentityServer\IdentityServerEMFDev.pfx", AppDomain.CurrentDomain.BaseDirectory), "KG0yM0At"),
        Factory = idSvrFactory,
        CorsPolicy = CorsPolicy.AllowAll,
        AuthenticationOptions = new AuthenticationOptions
        {
            IdentityProviders = ConfigureAdditionalIdentityProviders,
        },
    }
);

在 web.config 中有这个

<trace autoflush="true"
       indentsize="4">
  <listeners>
    <add name="myListener"
         type="System.Diagnostics.TextWriterTraceListener"
         initializeData="Trace.log" />
    <remove name="Default" />
  </listeners>
</trace>

客户端数据正确,因为我已成功使用隐式流程登录。 我错过了什么?这让我很紧张,我正在阅读 OAuth RFC,但我不明白为什么这不起作用。

【问题讨论】:

  • 我忘记添加范围参数了,但是在过去的请求中我使用了它,结果是一样的。

标签: asp.net oauth-2.0 openid identityserver3


【解决方案1】:

我尝试了新版本的 Postman(我不知道它的编号,但现在它作为 chrome 应用程序在桌面上运行),我从旧 Postman 版本中复制了值,现在一切正常。

POST /connect/token HTTP/1.1
Host: localhost:44302
Authorization: Basic ZXNtaWZhdm9yaXRvOmVzbWlmYXZvcml0bw==
Cache-Control: no-cache
Postman-Token: fc4acc63-29f2-6a37-b92c-b62034b13c29
Content-Type: application/x-www-form-urlencoded

grant_type=password&username=test&password=testuser&scope=write

这是生成的请求。 在 Postman 1 中,我有同样的东西(不包括 Postman-Token),它给了我 invalid_client。我什至使用了类似的 Firefox 工具,结果相同。 我不知道这怎么可能。 会不会是 chrome-extension:// 的问题?

我会回答自己,但如果有人知道这里发生了什么,我将永远感激不尽。

【讨论】:

    【解决方案2】:

    根据这篇文章似乎必须在标题中发送授权

    https://github.com/IdentityServer/IdentityServer3/issues/1520

    【讨论】:

      【解决方案3】:

      在我的情况下,我遇到了同样的问题,我注意到这是由于我的 HttpClient 设置了“自定义”授权标头。

      如果使用 IdentityModel 请求资源所有者密码令牌,请注意 Authorization 标头必须包含 base 64 中的client_id:client_secret

      在我的例子中,我设置了不同的授权标头,尽管正文值是正确的,IResourceOwnerPasswordValidator 甚至没有尝试验证请求。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-31
        • 2016-03-03
        • 1970-01-01
        • 2015-04-21
        • 1970-01-01
        • 2021-06-20
        • 2019-02-19
        相关资源
        最近更新 更多