首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASP.Net MVC应用程序默认为TLS1.0

ASP.Net MVC应用程序默认为TLS1.0
EN

Stack Overflow用户
提问于 2019-09-19 13:47:03
回答 1查看 1.1K关注 0票数 0

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

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

有没有人有过类似的问题和潜在原因的经验?我想在不硬编码协议的情况下修复这个问题,因为它不是未来的证据。

EN

回答 1

Stack Overflow用户

发布于 2019-09-19 14:43:08

我也遇到了类似的问题,结果只是简单地将其设置为配置:

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

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

显然,以逗号分隔的映射到枚举的事物列表作为设置有点不友好,但如果您愿意,可以设置某种映射或其他什么.它很适合我们的需要。

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

https://stackoverflow.com/questions/58012521

复制
相关文章

相似问题

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