首页 > 解决方案 > 如何使用 C# 勾选“TLS 1.2”(Internet 选项> 高级设置> 安全> TLS 1.2)

问题描述

我们的应用程序使用Webbrowser 控件/Internet Explorer 来显示一些网页。我们需要启用 TLS 1.2(Internet 选项->高级->安全->使用 TLS 1.2)来显示这些网页。现在,当禁用(默认禁用)TLS 1.2 选项时,我们在 Win 8 中面临一些问题。所以我们需要检查它是否被勾选,如果没有,我们需要在 C# 中以编程方式勾选它。我们已经尝试通过设置注册表值,但它没有帮助。有没有办法以编程方式勾选“Internet 选项->高级->安全->使用 TLS 1.2”。

标签: c#.netwebbrowser-controlinternet-explorer-10tls1.2

解决方案


您可以使用Registry.SetValue 方法来设置更改注册表并启用 TLS 1.2。

代码如下(需要添加“using Microsoft.Win32;”引用):

static void Main(string[] args)
{
    // The name of the key must include a valid root.
    const string userRoot = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings";
    const string subkey = "SecureProtocols";

    //get the registry value.
    string result = (Registry.GetValue(userRoot, subkey, "Return this default if NoSuchName does not exist")).ToString();
    Console.WriteLine(result);

    //Enable TLS 1.0 and TLS 1.2 
    Registry.SetValue(userRoot, subkey, 2176);

    Console.WriteLine("OK");
    Console.ReadKey();
}

有关注册表项值的更多详细信息,请参阅本文


推荐阅读