首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C#中验证连接安全协议

在C#中验证连接安全协议
EN

Stack Overflow用户
提问于 2018-05-25 15:35:19
回答 2查看 6.1K关注 0票数 1

我的应用程序连接到外部服务以接收数据。外部服务正在更新它们的安全协议,以排除TLS 1.0和更低版本。我已经在Global.asax中添加了以下内容:

代码语言:javascript
复制
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 |
                                       SecurityProtocolType.Tls11 |
                                       SecurityProtocolType.Tls;

但是,我想验证一下,我正在通过Tls 1.1或更高版本连接到外部服务。

是否有可能看到连接中使用的安全协议?我怀疑它存储在请求/响应对象的某个属性中。

代码语言:javascript
复制
var request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/json";
request.Headers["Device-Token"] = deviceId;
var response = request.GetResponse().GetResponseStream();

有人知道我在哪里能找到这些信息吗?或者是否有更好的方法来验证所使用的安全协议?

编辑

为了坚持更好的实践(根据Jf的评论),将设置连接协议的代码更改为:

代码语言:javascript
复制
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | 
                                        SecurityProtocolType.Tls11;
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-30 13:36:53

我发现PayPal也在改变它们的安全设置。

PayPal提供了一个API端点(https://tlstest.paypal.com/)来测试应用程序的安全协议,以确保它支持TLS1.2和HTTP1.1。

下面是我测试这个的方法:

MVC应用

./Global.asax.cs

代码语言:javascript
复制
...
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

代码语言:javascript
复制
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

代码语言:javascript
复制
$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/

票数 0
EN

Stack Overflow用户

发布于 2018-05-25 16:00:12

如果您只想检查连接实际使用的级别,只需在设置SecurityManager.SecurityProtocol值时删除不需要的协议即可。如果连接使用您排除的协议,则会出现异常。

请注意,应该避免以在SecurityProtocol中使用Global.asax的方式指定Global.asax,这是一种糟糕的做法。请参阅MSDN上的备注:

https://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.securityprotocol%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50532654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档