簡體   English   中英

將 WebService Endpoint 從 HTTP 更改為 HTTPS

[英]Change WebService Endpoint from HTTP to HTTPS

我真的是 .NET 的新手,我遇到了一個障礙(我猜)。

我有一個項目需要為我們使用的另一項服務更改一個地址,該地址從 HTTP 更改為 HTTPS。

我的問題是,對於此更改,我是否需要更改調用請求的方法的代碼,以便在其中加載證書,還是僅更改端點配置?

我已經嘗試了從NoneTransportWithMessageCredential的端點配置安全綁定,還將證書加載到機器存儲庫,但我不確定如何指定要發送的證書。

我猜您現在正在使用BasicHttpBinding ,並且需要從 HTTP 端點更改為需要通過 X509 證書進行驗證的 HTTPS 端點。 我假設您使用的是 C#。

如果您使用的是4.5 之前的任何 .NET Framework 版本,您可以執行以下操作:

var binding = new BasicHttpBinding(BasicHttpsSecurityMode.Transport);

如果您使用的是 .NET Framework 4.5 或更新版本,您可以這樣做:

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

假設您已導入派生自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