Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 标识服务器4正在获取“;无效的“授权”;将连接/令牌终结点部署到生产时出错_C#_Asp.net Core_Oauth 2.0_Identityserver4_Identity - Fatal编程技术网

C# 标识服务器4正在获取“;无效的“授权”;将连接/令牌终结点部署到生产时出错

C# 标识服务器4正在获取“;无效的“授权”;将连接/令牌终结点部署到生产时出错,c#,asp.net-core,oauth-2.0,identityserver4,identity,C#,Asp.net Core,Oauth 2.0,Identityserver4,Identity,我使用Identityserver4实现OAUTH2,服务器支持ResourceOwnerPassword和code流。我使用AWS的EC2托管应用程序进行生产 奇怪的是,即使这个应用程序在我的开发机器上也运行得很好,在部署到AWS之后,我一直得到这个无效的\u grant,我不知道出了什么问题 这是我的密码: services.AddIdentityServer() //.AddDeveloperSigningCredential() .AddS

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

奇怪的是,即使这个应用程序在我的开发机器上也运行得很好,在部署到AWS之后,我一直得到这个
无效的\u 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;
        }
    }
我知道输入的信息(客户端id、重定向url)都是正确的(都在本地正常工作)。一旦部署到生产环境中,这里会出现什么问题?证书是否在生产中不受信任,或者我无法为客户端和资源使用内存存储?我不认为这是由于
重定向\u url
造成的,因为即使我使用
密码
流,它甚至不需要
重定向\u url
,但在生产中仍然失败

注意:如果删除这一行
AddSigningCredential(Certificate.GetCertificate())
(不向identityserver4传递证书),我将获得相同的
“无效授权”
。因此,可能从我的开发机器导入AWS的证书无效?

打开日志后, 问题是
键集不存在
应用程序没有读取证书中私钥的权限。添加权限后,问题得到解决。
最初的
invalid\u grant
是如此误导…

有这个问题,当时我以我无限的智慧,将
accessToken
到期时间设置为
refreshttoken
到期时间,并且仅在
access
到期时才要求
刷新。

我假设您在生产中的评论
是指您的代码正在公共服务器(AWS)上运行。这意味着您需要使用HTTPS,请为您的域使用真正的SSL证书(无自签名)您不能将
localhost
用于
重定向uri
。您还需要在提供商的控制台中指定正确的
重定向uri
。@JohnHanley但我的证书用于Identityserver4颁发令牌,而不是用于使用HTTPS。它是一个x509证书,不是SSL证书(如果我理解正确的话)当我使用postman时,我不认为我需要一个真正的重定向uri,因为它实际上不会重定向到指定的url,url只是为了验证(也是我的理解)重定向uri是您令牌的回调。您的无效授予错误是由我提到的您需要更正的项引起的。我现在使用https和ACM生成的证书,并注册有效的回调uri,但它仍然不起作用。不过,出于令牌验证目的,我仍然使用自签名密钥。(我认为可以这样做,如许多identity server4文档所示)使用自签名证书可以对令牌进行签名和验证。Q)客户端代码在哪里运行(在同一台服务器上或在不同的计算机上)?重定向uri是将令牌传递给您的位置。如果客户端在不同的计算机上运行,则必须使用具有公共dns地址的URL。只有当客户端和OAuth服务器在同一台计算机上运行时,Localhost才起作用。
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