X509Certificate2认证问题

问题描述 投票:0回答:1

我正在与相关机构提供的证书密钥(.pfx)文件进行支付网关集成,当我在localhost上工作时,一切都按预期工作。但是,在Windows Server 2019中发布后,令牌生成过程出现了一些问题。

这是我们使用的令牌生成代码

RSACng key = new System.Security.Cryptography.RSACng();
            X509Certificate2 publicCert = new X509Certificate2(publicKeyLocation, "123", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);
            X509Certificate2 privateCert = null;
            X509Store store = new X509Store(StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);
            foreach (X509Certificate2 cert in store.Certificates)
            {
                var val1 = publicCert.GetCertHashString();
                if (cert.GetCertHashString() == publicCert.GetCertHashString())
                    privateCert = cert;
            }
            key = privateCert.GetRSAPrivateKey() as RSACng;
            byte[] signature = key.SignHash(hashValue, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
            key = (System.Security.Cryptography.RSACng)publicCert.GetRSAPublicKey();
            if (!key.VerifyHash(hashValue, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1))
                throw new CryptographicException();
            return signature;

这是我们从localhost调用api时得到的响应

Success Response Got In Localhost

这是在Windows Server 2019中发布后api的响应

Failure response after publishing the api

我正在与有关机构提供的证书密钥(.pfx)文件进行支付网关集成,而我在localhost上时,一切都按预期工作了。但是在...

c# asp.net-web-api operating-system x509certificate2 windows-server-2019
1个回答
1
投票
问题出在这里:X509Store store = new X509Store(StoreLocation.CurrentUser);那可以在您的PC上运行,因为您已将证书存储在CurrentUser存储下,但是将应用程序部署到Windows Server时,运行该应用程序的用户的证书存储中没有特定的证书。将证书安装到LocalMachine证书存储,然后从那里获取:
© www.soinside.com 2019 - 2024. All rights reserved.