gpt4 book ai didi

c# - 将 ssl 添加到 webapi...也添加到客户端?

转载 作者:太空宇宙 更新时间:2023-11-03 13:35:21 25 4
gpt4 key购买 nike

我正在尝试了解 SSL 基础知识。 (使用微软Azure云服务)在 Azure 中,我购买了 SSL 证书,并将其绑定(bind)到我的自定义域。似乎工作正常。我的问题是关于实现...

当从客户端进行 webapi 调用时,一些文档告诉我只需像这样进行调用...

httpClient.BaseAddress = new Uri("https://foobar.com/");

但我也发现有时我必须将证书添加到我进行调用的程序中。

//You must change the path to point to your .cer file location. 
X509Certificate Cert = X509Certificate.CreateFromCertFile("C:\\mycert.cer");
// Handle any certificate errors on the certificate from the server.
ServicePointManager.CertificatePolicy = new CertPolicy();
// You must change the URL to point to your Web server.
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://YourServer/sample.asp");
Request.ClientCertificates.Add(Cert);
Request.UserAgent = "Client Cert Sample";
Request.Method = "GET";
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

我的问题是,什么时候我只需将 https 添加到 httpclient,什么时候必须将证书添加到实际客户端?

最佳答案

My question is, when do I simply add the https to the httpclient, and when do I have to add the certificate to the actual client?

这是通过安全通道 (TLS) 进行的客户端 -> 服务器调用。这是每个人通常都会做的事情,也是您打开 HTTPS URL 时浏览器的工作方式:

httpClient.BaseAddress = new Uri("https://foobar.com/");

这是通过安全通道 (TLS) 进行的客户端 -> 服务器调用,该 channel 还执行 TLS 相互身份验证(也称为客户端证书身份验证)强>)。客户端必须向服务器证明其身份(通过提供证书,X509Certificate Cert),服务器向客户端也是如此 - 因此相互:

//You must change the path to point to your .cer file location. 
X509Certificate Cert = X509Certificate.CreateFromCertFile("C:\\mycert.cer");
// Handle any certificate errors on the certificate from the server.
ServicePointManager.CertificatePolicy = new CertPolicy();
// You must change the URL to point to your Web server.
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://YourServer");
Request.ClientCertificates.Add(Cert);
Request.UserAgent = "Client Cert Sample";
Request.Method = "GET";
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

您将使用第一个来执行安全调用,第二个(相互身份验证)来执行安全且经过身份验证调用(例如,您的 Web 应用程序调用后端 API您拥有的)。

实际上,它更像是通过验证身份确保安全,但我们在这里打开了一 jar 蠕虫,所以让我们就这样吧。

关于c# - 将 ssl 添加到 webapi...也添加到客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42099575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com