identity server4 getting quot;invalid_grantquot; error for the connect/token endpoint when deployed to production(身份服务器4在将连接/令牌终结点部署到生产环境时,获取连接/令牌终结点的Q;INVALID_GRANTQ错误)
问题描述
我使用Identityserver4实现OAUTH2,服务器支持ResourceOwnerPassword和code流。我使用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_url的password流,它仍然在生产中失败。
注意:如果删除此行AddSigningCredential(Certificate.GetCertificate())(不将证书传递给身份服务器4),我将得到相同的"invalid_grant。那么,从我的开发机器导入到AWS的证书可能无效?
推荐答案
打开日志后 问题是keyset does not exist
App无权读取证书中的私钥。添加权限后,问题就解决了。
首字母invalid_grant具有很强的误导性...
这篇关于身份服务器4在将连接/令牌终结点部署到生产环境时,获取连接/令牌终结点的&Q;INVALID_GRANT&Q错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:身份服务器4在将连接/令牌终结点部署到生产环境时,获取连接/令牌终结点的&Q;INVALID_GRANT&Q错误
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
