2009-06-09 63 views
10

我正在使用WatiN进行IE自动化项目。以编程方式将受信任的站点添加到Internet Explorer中

当点击要下载的文件,我得到的Internet Explorer信息栏下面:

为帮助保护您的安全, Internet Explorer已经从网上下载文件到您 阻止此 网站电脑。

为了下载报告,我可以手动添加网站到Internet Explorer的受信任站点列表,但我宁愿在.NET编程检查,看看是否该网站是可信的,并把它添加到列表如果不是。

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

+0

我不认为你可以...但如果你能我想看看怎么样! – marcgg 2009-06-09 20:39:24

+0

出于安全考虑,我不认为你可以。话虽如此,它们必须存储在*某处*。了解微软,在注册表中... – Powerlord 2009-06-09 20:42:11

+4

@R。 Bemrose,theres没有安全理由不允许它。如果一个恶意程序对HKCU有写入权限,这可能不是最糟糕的。如果*网站*可以将自己添加到受信任的站点,那么现在这会很糟糕。 – 2009-06-09 21:00:41

回答

12

看一看this

基本上它看起来好像所有你需要做的就是创造

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\DOMAINNAME 

注册表键,然后命名为“HTTP”与价值== REG_DWORD值2

+3

我会建议使用专用枚举TrustedZoneType { 联网= 1, TrustedSites = 2, 互联网= 3, RestrictedSites = 4 } – itsho 2011-10-08 21:29:30

9

下面是我在.NET中编写注册表项的过程。

谢谢你让我在正确的方向,本。

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; 
     } 
    } 
} 
5

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

在这种情况下,你必须使用另一种方法:

想象我想一个IP地址添加到受信任的站点:说10.0.1.13和我不在乎什么协议。

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

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

-1

如果一个网站可以自己添加到受信任的站点,现在这将是糟糕的。

我不太品质协议,只要在浏览器询问权限的用户,一个网站将自身添加到受信任的站点可以大大简化用户体验,用户信任域的能力并希望显示正确的页面。

另一种方法是用户必须手动进入Internet选项才能添加域,这对我的用户来说是不可行的。

我正在寻找一个php或javascript方法为网站添加自己,无论是通过一些IE浏览器API,或通过注册表,因为你已经如此有益地解释上面!

迄今已发现了这些可能的解决方案:

2

除了adding the domain to the Trusted Sites list来港上市,您可能还需要更改“受信任的站点”区域的设置“自动提示文件下载”。要做到这一点编程,修改键/值:

HKCU \软件\微软\的Windows \ CurrentVersion \互联网 设置\区\ 2 @ 2200

变化从值(禁用)至(启用)。下面是一些C#代码来做到这一点:

public void DisableForTrustedSitesZone() 
{ 
    const string ZonesLocation = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones"; 
    const int TrustedSiteZone = 2; 

    const string AutoPromptForFileDownloadsValueName = @"2200"; 
    const int AutoPromptForFileDownloadsValueEnable = 0x00;  // Bypass security bar prompt 

    using (RegistryKey currentUserKey = Registry.CurrentUser) 
    { 
     RegistryKey trustedSiteZoneKey = currentUserKey.OpenSubKey(string.Format(@"{0}\{1:d}", ZonesLocation, TrustedSiteZone), true); 
     trustedSiteZoneKey.SetValue(AutoPromptForFileDownloadsValueName, AutoPromptForFileDownloadsValueEnable, RegistryValueKind.DWord); 
    } 
} 
2

这里是增加信任的网站编程到IE浏览器的实现 - 基于即使风采的代码。它也支持域名和IP地址。限制是没有具体的协议可以定义,而只是使用“*”的所有协议。

// Source : http://support.microsoft.com/kb/182569 
static class IeTrustedSite 
{ 
    const string DOMAINS_KEY = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains"; 
    const string RANGES_KEY = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Ranges"; 

    const int TRUSTED_SITE_CODE = 0x2; 
    const string ALL_PROTOCOL = "*"; 
    const string RANGE_ADDRESS = ":Range"; 

    public static void AddSite(string address) 
    { 
     string[] segmentList = address.Split(new string[] {"."}, StringSplitOptions.None); 
     if (segmentList.Length == 4) 
      AddIpAddress(segmentList); 
     else 
      AddDomainName(segmentList); 
    } 

    static void AddIpAddress(string[] segmentList) 
    { 
     string ipAddress = segmentList[0] + "." + segmentList[1] + "." + segmentList[2] + "." + segmentList[3]; 
     RegistryKey rangeKey = GetRangeKey(ipAddress); 

     rangeKey.SetValue(ALL_PROTOCOL, TRUSTED_SITE_CODE, RegistryValueKind.DWord); 
     rangeKey.SetValue(RANGE_ADDRESS, ipAddress, RegistryValueKind.String); 
    } 

    static RegistryKey GetRangeKey(string ipAddress) 
    { 
     RegistryKey currentUserKey = Registry.CurrentUser; 
     for (int i = 1; i < int.MaxValue; i++) 
     { 
      RegistryKey rangeKey = currentUserKey.GetOrCreateSubKey(RANGES_KEY, "Range" + i.ToString()); 

      object addressValue = rangeKey.GetValue(RANGE_ADDRESS); 
      if (addressValue == null) 
      { 
       return rangeKey; 
      } 
      else 
      { 
       if (Convert.ToString(addressValue) == ipAddress) 
        return rangeKey; 
      } 
     } 
     throw new Exception("No range slot can be used."); 
    } 

    static void AddDomainName(string[] segmentList) 
    { 
     if (segmentList.Length == 2) 
     { 
      AddTwoSegmentDomainName(segmentList); 
     } 
     else if (segmentList.Length == 3) 
     { 
      AddThreeSegmentDomainName(segmentList); 
     } 
     else 
     { 
      throw new Exception("Un-supported server address."); 
     } 
    } 

    static void AddTwoSegmentDomainName(string[] segmentList) 
    { 
     RegistryKey currentUserKey = Registry.CurrentUser; 

     string domain = segmentList[0] + "." + segmentList[1]; 
     RegistryKey trustedSiteKey = currentUserKey.GetOrCreateSubKey(DOMAINS_KEY, domain); 

     SetDomainNameValue(trustedSiteKey); 
    } 

    static void AddThreeSegmentDomainName(string[] segmentList) 
    { 
     RegistryKey currentUserKey = Registry.CurrentUser; 

     string domain = segmentList[1] + "." + segmentList[2]; 
     currentUserKey.GetOrCreateSubKey(DOMAINS_KEY, domain); 

     string serviceName = segmentList[0]; 
     RegistryKey trustedSiteKey = currentUserKey.GetOrCreateSubKey(DOMAINS_KEY + @"\" + domain, serviceName); 

     SetDomainNameValue(trustedSiteKey); 
    } 

    static void SetDomainNameValue(RegistryKey subDomainRegistryKey) 
    { 
     object securityValue = subDomainRegistryKey.GetValue(ALL_PROTOCOL); 
     if (securityValue == null || Convert.ToInt32(securityValue) != TRUSTED_SITE_CODE) 
     { 
      subDomainRegistryKey.SetValue(ALL_PROTOCOL, TRUSTED_SITE_CODE, RegistryValueKind.DWord); 
     } 
    } 
} 

static class RegistryKeyExtension 
{ 
    public static RegistryKey GetOrCreateSubKey(this RegistryKey registryKey, string parentString, string subString) 
    { 
     RegistryKey subKey = registryKey.OpenSubKey(parentString + @"\" + subString, true); 
     if (subKey == null) 
      subKey = registryKey.CreateSubKey(parentString, subString); 

     return subKey; 
    } 

    public static RegistryKey CreateSubKey(this RegistryKey registryKey, string parentString, string subString) 
    { 
     RegistryKey parentKey = registryKey.OpenSubKey(parentString, true); 
     if (parentKey == null) 
      throw new Exception("BUG : parent key " + parentString + " is not exist."); 

     return parentKey.CreateSubKey(subString); 
    } 
} 
1

使用powershell很容易。

#Setting IExplorer settings 
Write-Verbose "Now configuring IE" 
#Add http://website.com as a trusted Site/Domain 
#Navigate to the domains folder in the registry 
set-location "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" 
set-location ZoneMap\Domains 

#Create a new folder with the website name 
new-item website/ -Force 
set-location website/ 
new-itemproperty . -Name * -Value 2 -Type DWORD -Force 
new-itemproperty . -Name http -Value 2 -Type DWORD -Force 
new-itemproperty . -Name https -Value 2 -Type DWORD -Force 
相关问题