简体   繁体   中英

IdentityServer4 unsupported_grant_type Error

I have been trying to get the simplest examples of IdentityServer4 to work for requesting access in pure code behind. I can get an access token when using a client request but not when doing a user login..

var discos = new DiscoveryClient(authority);
        var disco = await discos.GetAsync();

        if (disco.IsError)
        {
            Console.WriteLine(disco.Error);
            return null;
        }

        var tokenClient = new TokenClient(disco.TokenEndpoint, "ro.client", "secret");

        var tokenResponse = await tokenClient.RequestResourceOwnerPasswordAsync("username", "password", "api1");

This is the client making the request using user details. I get a perpetual unsupported_grant_type..

The server has it setup as:

 new Client
            {
                ClientId = "ro.client",
                AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,

                ClientSecrets =
                {
                    new Secret("secret".Sha256())
                },
                AllowedScopes = { "api1" }
            }

Can anyone please identify what Im mising. User can login using the front end quick start UI that the software offers and this is built in functionality.. Why wont it work if the company is valid.

Your ClientSecrets is configured for the Sha256 of "secret" not the literal string "secret".

Update your tokenClient to pass the Sha256 of "secret" instead of the literal string "secret"

var tokenClient = new TokenClient(disco.TokenEndpoint, "ro.client", "secret".Sha256());

Now resolved. I have managed to get this working using a newer version of the nuget package so may have been some conflict with what I was reading and old requirements.

For ref I have uploaded the working demo with Server, API and console client to github: https://github.com/PheonixProject/IdentityServer4Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM