为什么总是出错找不到请求的对象?请参阅我的评论代码中的跟踪详细信息。
我需要实现 WSS 客户端并使用证书检查 WSS 服务器的可用性。
但是当尝试创建 的对象时X509Certificate2
,出现错误:
找不到请求的对象
bool IsWssConnAlive(string WssUrl)
{
bool flg = false;
try
{
//string _certificateKeyStorePath = @"E:\bk\WSS\gateway\test\keystoreserver.der";
string _certificateKeyStorePath = @"E:\bk\WSS\gateway\test\keystoreserver.p12";
X509Certificate2 x509_1 = new X509Certificate2(_certificateKeyStorePath, "test@123");
/*
* Above X509Certificate2 statement aways giving error : "Cannot find the requested object.\r\n"}
* I tried .der, p21, .jks getting same error
* stack trace details :->
* at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.X509Certificates.X509Utils._QueryCertFileType(String fileName)
at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName, String password)
at Test_WssConnectivity.Program.IsWssConnAlive(String WssUrl, Int32 CheckTimeInSec) in E:\Development\SolutionForTestProject\Test_WssConnectivity\Program.cs:line 142
*/
ClientWebSocket websocket = new ClientWebSocket();
websocket.Options.ClientCertificates.Add(x509_1);
X509CertificateCollection xcol = new X509CertificateCollection();
xcol.Add(x509_1);
websocket.Options.ClientCertificates = xcol;
//connect to URL
websocket.ConnectAsync(new Uri(WssUrl), CancellationToken.None);
//Now checking actaul status
if (websocket.State == WebSocketState.Open)
{
flg = true;
}
else if (websocket.State == WebSocketState.Closed)
{
flg = false;
}
}
catch (Exception ex)
{
flg = false;
}
return flg;
}