【问题标题】:ASP.Net MVC application Defaults to TLS 1.0ASP.Net MVC 应用程序默认为 TLS 1.0
【发布时间】:2019-09-19 13:47:03
【问题描述】:

我们有一个 ASP.Net MVC 应用程序,它使用服务器到服务器的通信来检索一些信息。

当我们在 AWS 云中运行安装时,请求失败,因为默认情况下,WebRequest 使用我们在环境中禁用的 TLS 1.0。在另一个项目中使用相同的代码默认为 TLS 1.2。此外,在 ServicePointManager 中对协议进行硬编码可以解决此问题。

有没有人遇到过类似问题和根本原因?我想在不对协议进行硬编码的情况下解决这个问题,因为它不是面向未来的。

【问题讨论】:

  • 问题是默认值也被硬编码为 SSL 3 或 TLS 1.0。在某些时候,你必须硬编码一些东西,如果不是为了禁用协议变得过时。充其量,提供一些用于设置协议的配置选项,并在新协议出现时更新。
  • 问题是,根据微软自己的文档,在 .Net 4.6 中编译应用程序应该将默认设置为 TLS 1.2
  • 我不确定您引用的是什么 MS 文档,但请查看 ServicePointManager.SecurityProtocol (docs.microsoft.com/en-us/dotnet/api/…) 的文档 - 以下是一些重要细节。从 .NET Framework 4.7 开始,此属性的默认值为 SecurityProtocolType.SystemDefault。这允许......从操作系统继承默认的安全协议......
  • 对于从 .NET Framework 4.6.2 到 .NET Framework 的版本,没有为此属性列出默认值。 安全环境不断变化,默认协议和保护级别会随着时间的推移而改变,以避免已知的弱点。 默认值因机器配置、安装的软件和应用的补丁而异。
  • 最后 - “你的代码应该永远不要隐含地依赖于使用特定的保护级别,或者假设默认使用给定的安全级别。如果你的应用依赖于使用特定的安全级别,您必须明确指定该级别,然后检查以确保它确实在已建立的连接上使用。此外,您的代码应该设计为在面对更改时保持稳健支持哪些协议,因为此类更改通常是在没有提前通知的情况下进行的,以减轻新出现的威胁。"

标签: c# asp.net-mvc tls1.2 webrequest


【解决方案1】:

我遇到了类似的问题,最后只是将其设置为配置设置:


//read setting as comma-separated string from wherever you want to store settings
//e.g. "SSL3, TLS, TLS11, TLS12"
string tlsSetting = GetSetting('tlsSettings')

//by default, support whatever mix of protocols you want..
var tlsProtocols = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

if (!string.IsNullOrEmpty(tlsSetting))
{
    //we have an explicit setting, So initially set no protocols whatsoever.
    SecurityProtocolType selOpts = (SecurityProtocolType)0;

    //separate the comma-separated list of protocols in the setting.
    var settings = tlsSetting.Split(new[] { ',' });

    //iterate over the list, and see if any parse directly into the available
    //SecurityProtocolType enum values.  
    foreach (var s in settings)
    {
        if (Enum.TryParse<SecurityProtocolType>(s.Trim(), true, out var tmpEnum))
        {
            //It seems we want this protocol.  Add it to the flags enum setting
            // (bitwise or)
            selOpts = selOpts | tmpEnum;
        }
    }

    //if we've allowed any protocols, override our default set earlier.
    if ((int)selOpts != 0)
    {
        tlsProtocols = selOpts;
    }
}

//now set ServicePointManager directly to use our protocols:
ServicePointManager.SecurityProtocol = tlsProtocols;

这样,您可以启用/禁用特定协议,如果在枚举定义中添加或删除任何值,您甚至不需要重新访问代码。

显然,映射到枚举的以逗号分隔的列表作为设置有点不友好,但您当然可以设置某种映射或其他任何东西......它非常适合我们的需求。

【讨论】:

  • 我喜欢这种方法,我会建议我们在以后的版本中这样做。但是,它并没有解释根本问题的原因,这真的让我抓狂:)。
  • 更多细节在这里找到:stackoverflow.com/q/28286086/3658528
猜你喜欢
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 2019-10-06
  • 2012-12-31
  • 1970-01-01
  • 2018-11-21
  • 1970-01-01
  • 2011-03-25
相关资源
最近更新 更多