IdentityServer4 unsupported_grant_type错误

时间:2017-09-24 19:18:30

标签: c# identityserver4

我一直在尝试使用IdentityServer4的最简单示例来处理后面的纯代码中的请求访问。我在使用客户端请求时可以获取访问令牌,但在用户登录时却无法获取访问令牌。

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");

这是使用用户详细信息发出请求的客户端。 我得到一个永久的unsupported_grant_type ..

服务器将其设置为:

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

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

任何人都可以确定我在做什么。用户可以使用软件提供的前端快速启动UI进行登录,这是内置的功能..如果公司有效,为什么不能正常工作。

2 个答案:

答案 0 :(得分:1)

您的ClientSecrets配置为Sha256的“secret”而不是文字字符串“secret”。

更新您的tokenClient以传递“秘密”的Sha256而不是文字字符串“secret”

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

答案 1 :(得分:0)

现在已经解决了。 我已经设法使用更新版本的nuget包来解决这个问题,所以可能与我正在阅读的内容和旧的要求有些冲突。

对于ref,我已经将服务器,API和控制台客户端的工作演示上传到github: https://github.com/PheonixProject/IdentityServer4Demo