实现使用Web服务通过SSL进行C#的客户端?
-
21-08-2019 - |
题
所以,我已经有了一个服务引用添加到该调用是从Oracle提供的Web服务的一个C#控制台应用程序。
我就拥有了一切设置和它时,它不使用SSL(HTTP)的作品像桃子。我试图将其设置现在使用SSL,和我遇到的问题与它添加到服务引用(甚至Web引用)。例如,URL(HTTPS),该服务被曝光时,没有返回相应的Web方法时我尝试将其添加到Visual Studio。
基础连接已关闭:一个发送发生意外的错误。 收到了意想不到的EOF或从传输流0字节。 元数据包含无法解析的引用:“ https://srs204.mywebsite.ca:7776 / SomeDirectory /为MyWebService?WSDL '
我有另一个困惑是关于证书的管理和部署。我有大约1000,将需要使用这个小工具外部客户端的网站和他们需要为了安装在合适的证书存储证书连接到Web服务。不知道到处理这个最好的办法。他们是否需要在根存储?
我已经花了网络上不少时间找了不同的选项,但不能获得良好的清洁答案的任何地方。
要总结,我有几个问题在这里:
1)任何人有使用SSL在Visual Studio中建立Web服务的一些很好的链接?
2)应如何我注册证书?哪个店就应该存在吗?我可以使用类似CertMgr注册呢?
有得是一本好书/教程/不管它会告诉我如何设置这样的事情了共同的良好做法。我似乎无法找到它!
解决方案
好了,我已经想通了这一点。我花了更长的时间比我想谈谈,但我想分享我的解决方案,因为它是我的一个巨大的忌讳看标准。 “哦,我定了!谢谢!”帖子说离开大家挂在实际上发生了什么。
所以
在根本的问题是,在默认情况下Visual Studio 2008中使用TLS的SSL握手,而我试图连接到使用SSL3甲骨文/基于Java的web服务。
当您在Visual Studio 2008中使用 “添加服务引用...”,你的没有办法来指定该服务点管理安全协议应SSL3。
除非
您采取静态WSDL文档和使用Wsdl.exe用,以生成一个代理类一>。
wsdl /l:CS /protocol:SOAP /namespace:MyNamespace MyWebService.wsdl
然后,可以使用升C编译器一>把这一代理类到库文件(.dll),并把它添加到你的.NET项目“参考”。
csc /t:library /r:System.Web.Services.dll /r:System.Xml.dll MyWebService.cs
在这一点上,你还需要确保你已经包括在你的“参考” System.Web.Services为好。
现在,你应该能够调用Web服务,而不在代码中的一个问题。为了使它的工作你将需要一个神奇的一行代码添加实例化服务之前。
// We're using SSL here and not TLS. Without this line, nothing workie.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
好了,所以我感觉对自己非常深刻的印象的测试是在我开发框大。然后,我部署到另一个客户端箱,它不会重新连接由于权限/权限问题。这闻起来像证书给我(无论他们闻起来像)。要解决这个问题,我使用certmgr.exe 以注册为网站上本地计算机受信任的根证书。
certmgr -add -c "c:\someDir\yourCert.cer" -s -r localMachine root
这让我的证书分发给我们的客户网站,为用户自动安装它。我真不知道在不同版本的Windows“安全型”怎么会是关于自动登记证书像这样的,但它的伟大的工作至今。
希望这个答案可以帮助一些人。由于blowdart也为所有的帮助在这一个,并提供一些见解。
其他提示
这听起来像Web服务使用自签名证书。坦率地说,这不是最好的方法。
假设你是一个庞大的组织,它的内部,你可以设置你自己的受信任的证书颁发机构,这是用的 Active Directory的。从CA托管Oracle服务的服务器可以申请一个证书,您可以使用广告政策通过将其放置在机器存储的受信任的根信任您的内部CA的根证书。这将消除需要手动信任和接受的Web服务的证书。
如果客户端机器外部,然后你将不得不让人们把服务暴露无论是从著名的CA如Verisign,Thawte安全后,GeoTrust等等的一个或一部分购买一个“真正的”证书你的安装包的公证书,并在每台机器上机水平安装到受信任的根证书颁发机构。这具有的问题,例如没有办法撤销证书,但将删除的提示。
感谢这个伟大的小费,刚刚看了一下周围,在你的东西,你有去了很多好的想法。这是我的一点补充 - 我搞清楚webMethods的,它有你连接到(SSL3而不是TLS)相同的问题作为Oracle应用服务器(惊喜!)。你的做法伟大的工作,这是我编。
鉴于静态类“工厂”,提供这两个方便,花花公子项目:
/// <summary>
/// Used when dispatching code from the Factory (for example, SSL3 calls)
/// </summary>
/// <param name="flag">Make this guy have values for debugging support</param>
public delegate void CodeDispatcher(ref string flag);
/// <summary>
/// Run code in SSL3 -- this is not thread safe. All connections executed while this
/// context is active are set with this flag. Need to research how to avoid this...
/// </summary>
/// <param name="flag">Debugging context on exception</param>
/// <param name="dispatcher">Dispatching code</param>
public static void DispatchInSsl3(ref string flag, CodeDispatcher dispatcher)
{
var resetServicePoint = false;
var origSecurityProtocol = System.Net.ServicePointManager.SecurityProtocol;
try
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3;
resetServicePoint = true;
dispatcher(ref flag);
}
finally
{
if (resetServicePoint)
{
try { System.Net.ServicePointManager.SecurityProtocol = origSecurityProtocol; }
catch { }
}
}
}
然后消耗这些东西(如你毫无疑问已经猜到了,但放在一个击鼓这里反正):
var readings = new ArchG2.Portal.wmArchG201_Svc_fireWmdReading.wmdReading[] {
new ArchG2.Portal.wmArchG201_Svc_fireWmdReading.wmdReading() {
attrID = 1, created = DateTime.Now.AddDays(-1), reading = 17.34, userID = 2
},
new ArchG2.Portal.wmArchG201_Svc_fireWmdReading.wmdReading() {
attrID = 2, created = DateTime.Now.AddDays(-2), reading = 99.76, userID = 3
},
new ArchG2.Portal.wmArchG201_Svc_fireWmdReading.wmdReading() {
attrID = 3, created = DateTime.Now.AddDays(-5), reading = 82.17, userID = 4
}
};
ArchG2.Portal.Utils.wmArchG201.Factory.DispatchInSsl3(ref flag, (ref string flag_inner) =>
{
// creates the binding, endpoint, etc. programatically to avoid mucking with
// SharePoint web.config.
var wsFireWmdReading = ArchG2.Portal.Utils.wmArchG201.Factory.Get_fireWmdReading(ref flag_inner, LH, Context);
wsFireWmdReading.fireWmdReading(readings);
});
这是卓有成效的 - 当我得到更多的时间,我会解决线程问题(或没有)
由于我没有信誉发表评论,我想提一提,迫使SSL3垫Nadrofsky的答案和代码示例也是类似于
错误的解决方案,同时使发生错误 HTTP请求 https://开头XXXX /不管。 这可能是由于事实 服务器证书未配置 与HTTP.SYS在正确的HTTPS 案件。这也可以由引起 安全性错配结合 客户端和服务器之间。
只要使用
// We're using SSL here and not TLS. Without this line, nothing workie.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
如通过垫提及。经测试,在一个HTTPS的SAP NetWeaver PI服务器。谢谢!
垫,
我有这样的问题也和我有一种方法来避免使用certmgr.exe到证书添加到受信任的根在远程机器上。
X509Store store;
store = new X509Store("ROOT", StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Add(certificate);
在“证书对象”可以这样创建的:
X509Certificate2 certificate = new X509Certificate2("Give certificate location path here");