简体   繁体   English

将 WebService Endpoint 从 HTTP 更改为 HTTPS

[英]Change WebService Endpoint from HTTP to HTTPS

I'm really new to .NET and I have come to a roadblock (I guess).我真的是 .NET 的新手,我遇到了一个障碍(我猜)。

I have a project that needs changing one address for another service we use, this address changed from HTTP to HTTPS.我有一个项目需要为我们使用的另一项服务更改一个地址,该地址从 HTTP 更改为 HTTPS。

My question is, for this change, do I need to change the code of the method that invokes the request, so I load the certificate in there, or do I just change the endpoint config?我的问题是,对于此更改,我是否需要更改调用请求的方法的代码,以便在其中加载证书,还是仅更改端点配置?

I've tried the endpoint config security binding from None to TransportWithMessageCredential , also loading the certificate to the machine repository, but I'm not sure how do I specify what certificate I want to send.我已经尝试了从NoneTransportWithMessageCredential的端点配置安全绑定,还将证书加载到机器存储库,但我不确定如何指定要发送的证书。

I guess you are using BasicHttpBinding right now, and need to change from a HTTP endpoint to a HTTPS endpoint that requires validation through X509 certificate.我猜您现在正在使用BasicHttpBinding ,并且需要从 HTTP 端点更改为需要通过 X509 证书进行验证的 HTTPS 端点。 I'm assuming you're using C#.我假设您使用的是 C#。

If you're using any version of .NET Framework older than 4.5 , you can do:如果您使用的是4.5 之前的任何 .NET Framework 版本,您可以执行以下操作:

var binding = new BasicHttpBinding(BasicHttpsSecurityMode.Transport);

If you are using .NET Framework 4.5 or newer version, you can do like this:如果您使用的是 .NET Framework 4.5 或更新版本,您可以这样做:

var binding = new BasicHttpsBinding();
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

Assuming you've imported the WebService definition deriving from System.ServiceModel.ClientBase (like when you right-click the project in Visual Studio and choose Add > Service Reference), you can do something like this:假设您已导入派生自System.ServiceModel.ClientBase的 WebService 定义(就像在 Visual Studio 中右键单击项目并选择“添加”>“服务引用”一样),您可以执行以下操作:

var address = new EndpointAddress(serviceUrl);
var wsClient = new ServiceReference1.YourServiceClient(binding, address);
// x509Cert is a variable of type 'X509Certificate2'.
wsClient.ClientCredentials.ClientCertificate.Certificate = x509Cert;

// Take a look at: https://stackoverflow.com/a/49303859/
wsClient.Open();
wsClient.CallTheService();
wsClient.Close();

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

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