IdentityServer4最新版 出现invalid_scope

原贴解决方法地址:https://cyimt.net/Article/Details/66

首先请求地址:http://localhost:5000/connect/token

出现错误:

{
    "error": "invalid_request"
}

  请求图片:

 发现请求需要选择    application/x-www-form-urlencoded  而不是form-data

选择后,开始提示

 后经查找上面链接的帖子解决

config类代码修改,需要新加代码

namespace IdentityServiceCenter
{

    public class Config
    {
        public static IEnumerable<ApiResource> GetResources() {
            return new List<ApiResource> { 
                new ApiResource("api", "My Api") 
            };
        }

        public static IEnumerable<Client> GetClients()
        {
            return new List<Client> {
                new Client() {
                    ClientId = "client",

                    AllowedGrantTypes = GrantTypes.ClientCredentials,
                    ClientSecrets={
                        new Secret("secret".Sha256())
                    },
                    AllowedScopes={"api"}

                }
            };
        }

//上面是原来的代码

//下面是需要新加上方法,否则访问提示invalid_scope
        public static IEnumerable<ApiScope> ApiScopes =>
            new ApiScope[] { new ApiScope("api") };

        public static IEnumerable<IdentityResource> GetIdentityResources()
        {
            return new List<IdentityResource>
            {
                new IdentityResources.OpenId()
            };
        }
    }
}

Startup类中修改:

ConfigureServices方法需要修改
Configure方法中还是只需要开始的app.UseIdentityServer()就可以
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentityServer()
              .AddInMemoryApiResources(Config.GetResources())
              .AddInMemoryClients(Config.GetClients())

              //这个ApiScopes需要新加上,否则访问提示invalid_scope
              .AddInMemoryApiScopes(Config.ApiScopes)

              .AddDeveloperSigningCredential();


            services.AddControllers();
        }

再次请求成功:

原文地址:https://www.cnblogs.com/slsddy/p/14441653.html