身份服务器4在将连接/令牌终结点部署到生产环境时,获取连接/令牌终结点的&Q;INVALID_GRANT&Q错误 [英] identity server4 getting "invalid_grant" error for the connect/token endpoint when deployed to production

查看:87
本文介绍了身份服务器4在将连接/令牌终结点部署到生产环境时,获取连接/令牌终结点的&Q;INVALID_GRANT&Q错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Identityserver4实现OAUTH2,服务器支持ResourceOwnerPasswordcode流。我使用AWS的EC2托管用于生产的应用程序。

奇怪的是,即使这个应用程序在我的开发机器上也运行得很好,在部署到AWS后,我一直收到这个invalid_grant,我不知道哪里出了问题。

以下是我的代码:

services.AddIdentityServer()
            //.AddDeveloperSigningCredential()
            .AddSigningCredential(Certificate.GetCertificate())
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients())
            .AddTestUsers(Config.GetUsers());

new Client
        {
         ClientId = "client",
         ClientName = "client",
         ClientSecrets =
                {
                    new Secret("secret".Sha256())
                },
         RequireClientSecret = false,
         RedirectUris = new List<string>(new string[] { "https://www.getpostman.com/oauth2/callback", "http://localhost:8002", "http://192.168.1.5:8002","app.buyingagent:/oauthredirect"}),
         AllowedGrantTypes = GrantTypes.Code,
         //RequirePkce = true,
         AllowedScopes = { "api" },
         AllowOfflineAccess = true
        }

public static X509Certificate2 GetCertificate()
    {
        using (var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine))
        {
            store.Open(OpenFlags.OpenExistingOnly);
            var certs = store.Certificates.Find(X509FindType.FindBySubjectName, "cert", false);
            return certs.Count > 0 ? certs[0] : null;
        }
    }

我知道将信息保存在内存中不是一种好的做法,但我只想首先得到概念证明。对于传递给AddSigningCredential进行令牌签名的x509 certificate,我使用makecert在本地机器上创建了一个自唱证书,然后通过RDP将其导出到AWS中的可信存储。(在AWS的命令行中似乎不提供makecert)

我使用了以下命令: makecert -pe -ss MY -$ individual -n "CN=cert" -len 2048 -r

应用程序在本地运行Find,但在生产中,我不断收到这个"INVALID_GRANT"错误。(我使用Postman获取令牌)我可以访问connect/authorize终结点(在那里我可以输入客户端ID和密码)

流程在connect/authorize终结点失败。

错误消息如下:

POST http://{url}/connect/token
Request Headers:
    undefined:undefined
Request Body:
    grant_type:"authorization_code"
 code:"7cb58d345975af02332f2b67cb71958ba0a48c391e34edabd0d9dd1500e3f24e"
 redirect_uri:"https://www.getpostman.com/oauth2/callback"
 client_id:"client"
Response Headers:
    undefined:undefined
Response Body:
    error:"invalid_grant"
    invalid_grant
Error
我知道输入的信息(客户端ID、重定向URL)都是正确的(都在本地工作得很好),一旦部署到生产中,这里会出现什么问题?证书是否在生产中不受信任,或者我无法将内存存储用于客户端和资源?我不认为是因为redirect_url,因为即使我使用甚至不需要redirect_urlpassword流,它仍然在生产中失败。

注意:如果删除此行AddSigningCredential(Certificate.GetCertificate())(不将证书传递给身份服务器4),我将得到相同的"invalid_grant。那么,从我的开发机器导入到AWS的证书可能无效?

推荐答案

打开日志后 问题是keyset does not exist App无权读取证书中的私钥。添加权限后,问题就解决了。 首字母invalid_grant具有很强的误导性...

这篇关于身份服务器4在将连接/令牌终结点部署到生产环境时,获取连接/令牌终结点的&Q;INVALID_GRANT&Q错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆