0
votes

This is my ConfigureServices method of my Identity Server project.

    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>();
    }

When I follow the tutorial, the tutorial uses .AddProfileService<ProfileService>() but I could not find ProfileService. So I use DefaultProfileService. Could the error due to this?

and in my config.cs

public class Config { public static IEnumerable GetApiResources() { return new List { new ApiResource("myresourceapi", "My Resource API") { Scopes = {new Scope("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")
                }
            }
        };
    }
}

When I test it at Postman, I got the error invalid_request

enter image description here

I have tested another client id ClientId = "secret_client_id" and it works.

2

2 Answers

1
votes

If you are using AddTestUser then you don't need to add a profile service as it adds one for you that works with the test users:

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

    return builder;
}
0
votes

Found out that my problem is I issued a GET request which should be a POST request.