c# - 在 C# 中使用带有 SSL 证书的 Rest 服务

标签 c# rest ssl certificate httprequest

我正在尝试使用休息服务来读取 C# 中的事件 MQ 消息。该服务有一个没有密码的 SSL 证书。当我在浏览器(chrome,reSTLet 客户端)中使用服务 url 而不是在 postman 中时,我可以成功获得响应。在访问 url 之前,我已将证书添加到 Chrome。

这是一个 HTTPS 服务。

我必须使用 SSL 证书和我的 http 请求来调用该服务。我尝试过 WebRequestHandler、HttpWebRequest、HttpClient,但没有成功。

当我这样做时,我收到了以下错误。

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

string url =  System.Configuration.ConfigurationManager.AppSettings["ActiveMQDev"].ToString();
string certFileName = System.Configuration.ConfigurationManager.AppSettings["SSLCertPath"].ToString();

// create HTTP web request with proper content type
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ContentType = "application/json;charset=UTF8";
X509Certificate2 cert = new X509Certificate2(certFileName);
request.ClientCertificates.Add(cert);
request.PreAuthenticate = true;
WebResponse response;


using (new ServerCertificateValidationScope(request, delegate { return true; }))
{
    // call the web service and get response
    response = request.GetResponse();   
}

if (response != null)
{
    //handle the response
}

这抑制了异常,但我得到了

RemoteCertificateNameMismatch

回调错误(发件人、证书、链、错误)。

当我没有调用抑制 SSL 错误时,我得到一个

401 unauthorized response from the server.

非常感谢任何帮助。谢谢你!

最佳答案

所以我们回到给我们证书的团队那里,发现它有问题!我们现在获得了带有密码的证书及其 pfx 文件。我们正在直接访问队列,现在在请求中附加了证书。这似乎已经解决了进入队列并访问其中的消息的问题。我们现在遇到的唯一问题是消息没有出队,但我想这就是 AMQ 团队的头疼问题。如果有人需要直接使用NMS api访问AMQ的代码,请给我发消息。我可以寄 sample 。感谢你的帮助。真的很感激。

关于c# - 在 C# 中使用带有 SSL 证书的 Rest 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44403229/

相关文章:

spring - 在 Spring Boot 中使用 Jersey 代替 Spring MVC 来实现 REST API 有什么优势?

java - 在java中使用SSO身份验证使用服务?

android - 使 Android 应用程序使用 FIPS 140-2 验证加密

ssl - 没有 SSL 的 Chrome 上的 WebSocket 连接失败

c# - Caliburn.Micro是否支持PasswordBox?

c# - 如何使 DragDrop.DoDragDrop 异步?

java - 未能关闭 Spring Boot 安全配置

c# - ASP.Net WebApi : How to load additional controllers at runtime

c# - 如何在AppDomain中找到尚未加载的类型?

c++ - 当与服务器的连接未保留时使用 SSL