【问题标题】:Invalid HTTP request for token endpoint in IdentityServer4IdentityServer4 中对令牌端点的 HTTP 请求无效
【发布时间】:2017-07-07 14:23:41
【问题描述】:

使用带有调试跟踪的 IDS4,日志只给了我一点点继续。当我这样发布时,我收到了上述错误(对令牌端点的 HTTP 请求无效):

Request starting HTTP/1.1 POST http://localhost:5000/connect/token?grant_type=password&client_id=resClient&client_secret=TopSecret&username=admin&password=admin123

在客户端(网络浏览器)我只是得到

{ "error": "invalid_request" }

整个调试日志如下所示:

dbug: Microsoft.AspNetCore.Server.Kestrel[1]
      Connection id "0HL2NGBR0Q1O4" started.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 POST http://localhost:5000/connect/token?grant_type=password&client_id=resClient&client_secret=TopSecret&username=admin&password=admin123  0
dbug: Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware[9]
      AuthenticationScheme: idsrv was not authenticated.
dbug: IdentityServer4.Hosting.EndpointRouter[0]
      Request path /connect/token matched to endpoint type Token
dbug: IdentityServer4.Hosting.EndpointRouter[0]
      Mapping found for endpoint: Token, creating handler: IdentityServer4.Endpoints.TokenEndpoint
info: IdentityServer4.Hosting.IdentityServerMiddleware[0]
      Invoking IdentityServer endpoint: IdentityServer4.Endpoints.TokenEndpoint for /connect/token
trce: IdentityServer4.Endpoints.TokenEndpoint[0]
      Processing token request.
warn: IdentityServer4.Endpoints.TokenEndpoint[0]
      Invalid HTTP request for token endpoint
trce: IdentityServer4.Hosting.IdentityServerMiddleware[0]
      Invoking result: IdentityServer4.Endpoints.Results.TokenErrorResult
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 348.2168ms 400 application/json
dbug: Microsoft.AspNetCore.Server.Kestrel[9]
      Connection id "0HL2NGBR0Q1O4" completed keep alive response.

我的代码非常稀疏。这是 Startup.cs:

namespace IDServer4Test
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddIdentityServer()
            .AddTemporarySigningCredential()
            .AddInMemoryClients(Configurations.Clients.GetClients())
            .AddInMemoryApiResources(Configurations.Scopes.GetApiResources());
            services.AddTransient<IResourceOwnerPasswordValidator, Configurations.ResourceOwnerPasswordValidator>();
            services.AddTransient<IProfileService, Configurations.ProfileService>();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(LogLevel.Trace);

            app.UseIdentityServer();
        }
    }
}

Clients.cs:

public class Clients
{
    public static IEnumerable<Client> GetClients()
    {
        return new List<Client>
    {
        new Client
        {
            ClientId = "resClient",
            ClientSecrets = { new Secret("TopSecret".Sha256()) },

            AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,
            AllowedScopes = { "myAPI" }
        }
    };
    }
}

还有 Scopes.cs:

public class Scopes
{
    public static IEnumerable<ApiResource> GetApiResources()
    {
        return new List<ApiResource>
        {
            new ApiResource("myAPI", "My API")
        };
    }
}

我应该从 IdentityServer 得到一个令牌响应,但只是不断地返回这个错误。有什么想法吗?

【问题讨论】:

    标签: c# token endpoint identityserver4


    【解决方案1】:

    这是对令牌端点的无效 HTTP 请求;)

    您检查过规格吗? https://www.rfc-editor.org/rfc/rfc6749#section-4.3.2

    参数在 POST 正文中传递。

    在 C# 中请求令牌的最简单方法是使用 IdentityModel 客户端库 https://github.com/IdentityModel/IdentityModel2

    【讨论】:

    • 根据 RFC,我只需要 grant_type、用户名和密码。范围是可选的。无论如何,使用这些参数调整请求仍然不能解决问题。我想我错过了一些代码。也许 IdentityModel 库有我需要的东西,但我对这项技术有点陌生,所以我无法弄清楚如何集成它。
    • 是的。但作为一个表格帖子。不作为查询字符串参数。
    • 我需要能够在没有 Web 表单的情况下执行此查询。你是说我使用了错误的方法来达到我想要的结果?
    • 是的。规范要求表单 POST 带有帖子正文中的参数。
    • 好的,将帖子更改为使用 x-www-form-urlencoded 可以使其成功发布。不幸的是,现在我有一个“invalid_client”。不过,我猜这可能完全是另一个问题。感谢您的帮助!
    【解决方案2】:

    您没有包括scope,您在发帖时需要令牌。在您的请求中包含&amp;scope=myAPI,我认为您的请求将是有效的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-25
      • 1970-01-01
      • 2017-11-20
      • 2012-06-08
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      相关资源
      最近更新 更多