简体   繁体   English

ICerfiticatePolicy和ServicePoint

[英]ICerfiticatePolicy and ServicePoint

So I'm using the PayPal API. 所以我正在使用PayPal API。 They require bigger companies to send an X509Certificate along with each SOAP API request. 他们要求更大的公司与每个SOAP API请求一起发送X509证书。 I've never heard of using a cert, it's always been just send the API signature along with an API request. 我从未听说过使用证书,它一直只是随API请求一起发送API签名。

So I first created a class called Cerficate that implements the .NET ICerfiticatePolicy . 因此,我首先创建了一个名为Cerficate的类,该类实现了.NET ICerfiticatePolicy One of the member methods, really the only one you have to implement is: 成员方法之一,实际上唯一需要实现的是:

System.Net.ICertificatePolicy.CheckValidationResult(System.Net.ServicePoint, System.Security.Cryptography.X509Certificates.X509Certificate, System.Net.WebRequest, int)

So far I'm having trouble really understanding what to pass to this method. 到目前为止,我很难真正理解该方法要传递的内容。 I guess the method simply validates that the Cerfiticate is valid. 我猜该方法只是证明Cerfiticate有效。 So I'm not sure what ServicePoint is and what to pass into it. 因此,我不确定ServicePoint是什么以及传递给它的内容。 I assumed it was my web service reference and a proxy class within such as the PayPalAPIAAInterfaceClient 我以为这是我的Web服务引用以及其中的代理类,例如PayPalAPIAAInterfaceClient

I also see a very old example using ServicePointManager.S for something but I don't understand it, even after looking at MSDN. 我也看到了一个使用ServicePointManager.S的非常老的示例,但即使看了MSDN,我也不理解。 So I guess you are to use ServicePointManager.ServerCertificateValidationCallback and I suppose set the callback to the CheckValidationResult? 所以我猜您将使用ServicePointManager.ServerCertificateValidationCallback,并且我想将回调设置为CheckValidationResult吗? If so, when do you do this? 如果是这样,您什么时候这样做? It's just very confusing to me. 这让我很困惑。

Also, So I guess I create an instance of my Certificate class and set the certificate properties by reading the P12 certificate from my disk and then pass in that to this method to check if it's valid? 另外,所以我想我创建了Certificate类的实例,并通过从磁盘读取P12证书来设置证书属性,然后将其传递给此方法以检查其是否有效? I guess that's right. 我想是对的。

I'm still trying to figure out this whole thing and I'm really stuck on the ServicePoint as well as WebRequest because really I'm using a proxy class in PayPal which does the under the hood sending of the request. 我仍在尝试弄清楚这件事,我真的在ServicePoint和WebRequest上都卡住了,因为实际上我在PayPal中使用了代理类,它在后台发送请求。 So I don't see how I can even pass in type WebRequest because I'm using a proxy method for that anyway. 因此,我什至看不到如何传递WebRequest类型,因为无论如何我都在使用代理方法。 So what would I even pass for the WebRequest param? 那么我什至会通过WebRequest参数呢? I'm using a SOAP API WSDL, not NVP here so I'm not for example creating an HttpWebRequest variable like you do with REST services in order to send the API request over Http. 我使用的是SOAP API WSDL,而不是NVP,因此,例如,我不会像创建REST服务那样创建HttpWebRequest变量,以便通过Http发送API请求。

so far here's what I've tried: 到目前为止,这是我尝试过的方法:

    PayPalAPIAAInterfaceClient client = new PayPalAPIAAInterfaceClient();
    Certificate x509Certificate = new Certificate();
    ServicePointManager.ServerCertificateValidationCallback = x509Certificate.CheckValidationResult();
    client.ClientCredentials.ClientCertificate.Certificate = x509Certificate;

the problem is, what do I pass in for the ServicePiont and the rest of the params for CheckValidationResult?? 问题是,我应该为ServicePiont和CheckValidationResult的其余参数传递什么? I don't even know if I'm calling this right. 我什至不知道我是否打这个电话。

It's certainly not unheard of and in fact fairly common to secure SOAP services with X.509 certificates using the WS-Security spec - in fact, we do this for all of our internal and external web services. 使用WS-Security规范使用X.509证书保护SOAP服务当然不是闻所未闻,并且实际上是相当普遍的-实际上,我们对所有内部和外部Web服务都执行此操作。 All web service frameworks including WCF are specifically designed to make this as easy as possible. 包括WCF在内的所有Web服务框架都经过专门设计,以使其尽可能地容易。

You should never have to use the ServicePointManager or ICertificatePolicy with a SOAP service using WS-Security. 您永远不必将ServicePointManagerICertificatePolicy与使用WS-Security的SOAP服务一起使用。 Unless there's something truly bizarre about PayPal's API, I think you're on the wrong track with that. 除非贝宝(PayPal)的API确实存在真正的怪异,否则我认为您的做法是错误的。 All you have to do in WCF is this: 您在WCF中要做的就是:

var client = new PayPalAPIInterfaceClient();
X509Certificate2 certificate = (...);
client.ClientCredentials.ClientCertificate.Certificate = certificate;
client.AddressVerify(...);  // or whatever method you want to call

You don't even really need to write this code; 您甚至根本不需要编写此代码。 if you have the certificate installed in the server's certificate store then you just edit the binding and behavior elements of the app.config - or use the WCF Service Configuration Editor, which is a lot easier. 如果您已将证书安装在服务器的证书存储区中,则只需编辑app.config的绑定和行为元素-或使用WCF服务配置编辑器,这会容易得多。

Of course, in order to do this you have to have an X.509 certificate, and PayPal has to know about it. 当然,要执行此操作,您必须拥有 X.509证书,并且PayPal必须了解它。 You can't just write new X509Certificate2() . 您不能只编写new X509Certificate2() You need to have a .pfx or .p12 file somewhere or, as mentioned above, have the certificate physically installed (this is the easiest way and the most secure because you're not hard-coding a password). 您需要在某个位置安装.pfx.p12文件,或者如上所述,已物理安装证书(这是最简单,最安全的方法,因为您没有对密码进行硬编码)。 And you need to upload the public key to PayPal. 并且您需要将公钥上载到PayPal。

You might be able to use OpenSSL to create a cert. 可能可以使用OpenSSL创建证书。 PayPal's EWP page suggests that they'll accept these and gives instructions on how to create them, although it's not entirely clear whether or not the same process can be used for their SOAP API. PayPal的EWP页面建议他们接受这些内容,并提供有关如何创建它们的说明,尽管尚不清楚是否可以将相同的过程用于其SOAP API。 It could be that they require a "real" certificate from Verisign, Thawte, etc. - I would try OpenSSL first and see, or just ask them. 可能是他们需要Verisign,Thawte等提供的“真实”证书。-我会先尝试OpenSSL并查看,或者问他们。

There's a pretty comprehensive guide to the whole process here - you'll probably want to skip the sections on generating the certificate unless you have a Microsoft CA somewhere. 有一个非常全面的指导,整个过程在这里 -你可能会想跳过生成证书的部分,除非你有一个微软CA地方。 Again, for that part, you'll probably want to try using the OpenSSL utility instead and follow PayPal's instructions, then install the cert on your server and skip to step 7 of that guide. 再次,对于那一部分,您可能想要尝试改用OpenSSL实用程序并按照PayPal的说明进行操作,然后将证书安装在服务器上,并跳至该指南的第7步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM