【问题标题】:invalid_request 400 bad requestinvalid_request 400 错误请求
【发布时间】:2020-11-18 00:56:31
【问题描述】:

这是我的 Identity Server 项目的 ConfigureServices 方法。

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentityServer()
      .AddDeveloperSigningCredential()
      .AddOperationalStore(options =>
      {
          options.EnableTokenCleanup = true;
          options.TokenCleanupInterval = 30; // interval in seconds
      })
      .AddInMemoryApiResources(Config.GetApiResources())
      .AddInMemoryClients(Config.GetClients())
        .AddTestUsers(Config.GetUsers())
        .AddProfileService<DefaultProfileService>();
    }

当我按照教程进行操作时,教程使用.AddProfileService&lt;ProfileService&gt;(),但我找不到ProfileService。所以我使用DefaultProfileService。会不会是这个错误?

在我的config.cs

公共类配置 { 公共静态 IEnumerable GetApiResources() { 返回新列表 { new ApiResource("myresourceapi", "我的资源 API") { 范围 = {新范围(“apiscope”)} } }; }

    public static IEnumerable<Client> GetClients()
    {
        return new[]
        {
            // for public api
            new Client
            {
                ClientId = "secret_client_id",
                AllowedGrantTypes = GrantTypes.ClientCredentials,
                ClientSecrets =
                {
                    new Secret("secret".Sha256())
                },
                AllowedScopes = { "apiscope" }
            },
            new Client
            {
                ClientId = "secret_user_client_id",
                AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
                ClientSecrets =
                {
                    new Secret("secret".Sha256())
                },
                AllowedScopes = { "apiscope" }
            }
        };
    }

    public static List<TestUser> GetUsers()
    {
        return new List<TestUser>
        {
            new TestUser
            {
                SubjectId = "1",
                Username = "user",
                Password = "user",
                Claims = new[]
                {
                    new Claim("roleType", "CanReaddata")
                }
            },
            new TestUser
            {
                SubjectId = "2",
                Username = "admin",
                Password = "admin",
                Claims = new[]
                {
                    new Claim("roleType", "CanUpdatedata")
                }
            }
        };
    }
}

当我在 Postman 测试它时,我收到了错误 invalid_request

我已经测试了另一个客户端 ID ClientId = "secret_client_id",它可以工作。

【问题讨论】:

    标签: identityserver4


    【解决方案1】:

    如果您使用的是AddTestUser,那么您不需要添加配置文件服务,因为它会为您添加一个与测试用户一起使用的服务:

    public static IIdentityServerBuilder AddTestUsers(this IIdentityServerBuilder builder, List<TestUser> users)
    {
        builder.Services.AddSingleton(new TestUserStore(users));
        builder.AddProfileService<TestUserProfileService>();
        builder.AddResourceOwnerValidator<TestUserResourceOwnerPasswordValidator>();
    
        return builder;
    }
    

    【讨论】:

      【解决方案2】:

      发现我的问题是我发出了一个GET 请求,它应该是一个POST 请求。

      【讨论】:

        猜你喜欢
        • 2018-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多