我的应用程序连接到外部服务以接收数据。外部服务正在更新它们的安全协议,以排除TLS 1.0和更低版本。我已经在Global.asax中添加了以下内容:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls;但是,我想验证一下,我正在通过Tls 1.1或更高版本连接到外部服务。
是否有可能看到连接中使用的安全协议?我怀疑它存储在请求/响应对象的某个属性中。
var request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/json";
request.Headers["Device-Token"] = deviceId;
var response = request.GetResponse().GetResponseStream();有人知道我在哪里能找到这些信息吗?或者是否有更好的方法来验证所使用的安全协议?
编辑
为了坚持更好的实践(根据Jf的评论),将设置连接协议的代码更改为:
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 |
SecurityProtocolType.Tls11;发布于 2018-05-30 13:36:53
我发现PayPal也在改变它们的安全设置。
PayPal提供了一个API端点(https://tlstest.paypal.com/)来测试应用程序的安全协议,以确保它支持TLS1.2和HTTP1.1。
下面是我测试这个的方法:
MVC应用
./Global.asax.cs
...
protected void Application_Start()
{
...
// Add Tls 1.1 and 1.2 to security protocol list (without removing defaults)
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls1.2 | SecurityProtocolType.Tls1.1
}
..../Controllers/TestConnection.cs
using System;
using System.IO;
using System.Net;
using System.Web.Http;
namespace MyMVCApplication.Controllers
{
public class TestConnectionController : ApiController
{
public string Get()
{
var url = new Uri("https://tlstest.paypal.com/");
var request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/json";
var response = request.GetResponse().GetResponseStream();
if (response != null)
{
string output;
using (var reader = new StreamReader(response))
{
output = reader.ReadToEnd();
}
return output;
}
return null;
}
}
}运行应用程序之后,您可以在本地连接到它(我使用PowerShell完成了它),并从PayPal端点接收响应。
PowerShell
$url = "http://localhost:60023/api/TestConnection"
Invoke-WebRequest -Uri $url -Headers @{Authorization = "Basic $credentials"} | ConvertFrom-Json如果您在PalPal_Connection_OK中将ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12添加到Application_Start()中,您将收到确认消息Application_Start
如果您的连接不支持TLS 1.2或HTTP/1.1,您将收到一个400错误。
如需更多信息,请访问网址:https://www.paypal-notice.com/en/TLS-1.2-and-HTTP1.1-Upgrade/。
发布于 2018-05-25 16:00:12
如果您只想检查连接实际使用的级别,只需在设置SecurityManager.SecurityProtocol值时删除不需要的协议即可。如果连接使用您排除的协议,则会出现异常。
请注意,应该避免以在SecurityProtocol中使用Global.asax的方式指定Global.asax,这是一种糟糕的做法。请参阅MSDN上的备注:
https://stackoverflow.com/questions/50532654
复制相似问题