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

再次請求成功:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM