以编程方式将可信站点添加到Internet Explorer

Eve*_*ien 11 .net internet-explorer automation watin trusted-sites

我正在使用WatiN进行IE自动化项目.

单击要下载的文件时,我在Internet Explorer信息栏中显示以下内容:

为了帮助保护您的安全,Internet Explorer阻止此站点将文件下载到您的计算机.

为了下载报告,我可以手动将站点添加到Internet Explorer的受信任站点列表中,但我更愿意在.NET中以编程方式检查该站点是否可信并将其添加到列表中(如果不是).

仅供参考,我目前正在使用IE7.

Ben*_*ehn 13

看看这个

基本上看起来你只需创建注册表项即可

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\DOMAINNAME
Run Code Online (Sandbox Code Playgroud)

然后是一个名为"http"的REG_DWORD值,其值为== 2

  • 我建议使用私有枚举TrustedZoneType {Intranet = 1,TrustedSites = 2,Internet = 3,RestrictedSites = 4} (3认同)

Eve*_*ien 10

这是我在.NET中编写注册表项时所实现的实现.

感谢你让我朝着正确的方向前进,Ben.

using System;
using System.Collections.Generic;
using Microsoft.Win32;


namespace ReportManagement
{
    class ReportDownloader
    {
        [STAThread]
        static void Main(string[] args)
        {

            const string domainsKeyLocation = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains";
            const string domain = @"newsite.com";
            const int trustedSiteZone = 0x2;

            var subdomains = new Dictionary<string, string>
                                 {
                                     {"www", "https"},
                                     {"www", "http"},
                                     {"blog", "https"},
                                     {"blog", "http"}
                                 };

            RegistryKey currentUserKey = Registry.CurrentUser;

            currentUserKey.GetOrCreateSubKey(domainsKeyLocation, domain, false);

            foreach (var subdomain in subdomains)
            {
                CreateSubdomainKeyAndValue(currentUserKey, domainsKeyLocation, domain, subdomain, trustedSiteZone);
            }

            //automation code
        }

        private static void CreateSubdomainKeyAndValue(RegistryKey currentUserKey, string domainsKeyLocation, 
            string domain, KeyValuePair<string, string> subdomain, int zone)
        {
            RegistryKey subdomainRegistryKey = currentUserKey.GetOrCreateSubKey(
                string.Format(@"{0}\{1}", domainsKeyLocation, domain), 
                subdomain.Key, true);

            object objSubDomainValue = subdomainRegistryKey.GetValue(subdomain.Value);

            if (objSubDomainValue == null || Convert.ToInt32(objSubDomainValue) != zone)
            {
                subdomainRegistryKey.SetValue(subdomain.Value, zone, RegistryValueKind.DWord);
            }
        }
    }

    public static class RegistryKeyExtensionMethods
    {
        public static RegistryKey GetOrCreateSubKey(this RegistryKey registryKey, string parentKeyLocation, 
            string key, bool writable)
        {
            string keyLocation = string.Format(@"{0}\{1}", parentKeyLocation, key);

            RegistryKey foundRegistryKey = registryKey.OpenSubKey(keyLocation, writable);

            return foundRegistryKey ?? registryKey.CreateSubKey(parentKeyLocation, key);
        }

        public static RegistryKey CreateSubKey(this RegistryKey registryKey, string parentKeyLocation, string key)
        {
            RegistryKey parentKey = registryKey.OpenSubKey(parentKeyLocation, true); //must be writable == true
            if (parentKey == null) { throw new NullReferenceException(string.Format("Missing parent key: {0}", parentKeyLocation)); }

            RegistryKey createdKey = parentKey.CreateSubKey(key);
            if (createdKey == null) { throw new Exception(string.Format("Key not created: {0}", key)); }

            return createdKey;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Dun*_*nkv 6

很高兴我遇到了你的帖子.我唯一可以添加到优秀贡献的是,只要URI包含IP地址,即地址不是完全限定的域名,就会使用不同的注册表项.

在这种情况下,您必须使用替代方法:

想象一下,我希望为受信任的站点添加一个IP地址:比如10.0.1.13,我不关心什么协议.

在HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Ranges下,我创建了一个键,例如"Range1"和创建以下值的内部:

名称为"*"且值为0x2的DWORD(对于所有协议(*)和可信站点(2))名称为":Range"且值为"10.0.1.13"的字符串