X509Certificate.loadStoreFromFile():必须信任该计算机的委托书,并且必须配置当前的用户



我在尝试加载p12证书文件在C#MVC Web应用程序中时面临问题。

加载证书时我们遇到的错误是:必须信任委托的计算机,并且必须配置当前用户帐户以允许委托。

加载证书的代码:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
var handler = new WebRequestHandler();
var certificate = new X509Certificate2Collection();
certificate.Import(@"D:certificate.p12", "password", X509KeyStorageFlags.DefaultKeySet);
handler.ClientCertificates.AddRange(certificate);
handler.ServerCertificateValidationCallback = ValidateServerCertificate;
var client = new HttpClient(handler)
{
    BaseAddress = new Uri(chargeCodeServer)
};

我们在以下行有一个例外:

certificate.Import(@"D:certificate.p12", "password", X509KeyStorageFlags.DefaultKeySet);

堆栈跟踪:

The requested operation cannot be completed. The computer must be trusted for delegation and the current user account must be configured to allow delegation.
at System.Security.Cryptography.X509Certificates.X509Certificate2Collection.LoadStoreFromFile(String fileName, String password, UInt32 dwFlags, Boolean persistKeyContainers)
at System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Import(String fileName, String password, X509KeyStorageFlags keyStorageFlags)

相同的代码在控制台应用程序中运行良好。

通过HTTPS调用Web API的证书已加载。

请让我知道是否需要任何信息。

我有相同的错误,对我而言,这就是:

certificate = new X509Certificate2(keyCertificateFilePath, keyCertificatePassword, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);

如果其他人仍在面对此问题,可以访问运行Web应用程序的帐户的证书。

与控制台应用程序不同,要启用ASP.NET Web应用程序使用客户端证书,必须在本地机器商店中安装客户端证书。当您在本地机器商店中安装客户端证书时,客户证书仅适用于管理员组中的用户帐户和安装客户端证书的用户。因此,您必须授予用于运行ASP.NET Web应用程序的用户帐户的客户端证书。

最新更新