2009-06-09 26 views
10

Tôi đang thực hiện dự án tự động hóa bằng IE bằng WatiN.Thêm các trang web đáng tin cậy theo chương trình vào Internet Explorer

Khi một tập tin được tải về được nhấp, tôi nhận được sau vào thanh trình duyệt Internet Explorer Thông tin:

Để giúp bảo vệ an ninh của mình, Internet Explorer đã chặn trang web này từ tập tin tải về để bạn máy vi tính.

Để tải xuống báo cáo, tôi có thể thêm trang web vào danh sách các trang web đáng tin cậy của Internet Explorer theo cách thủ công, nhưng tôi muốn kiểm tra theo lập trình trong .NET để xem trang web có đáng tin cậy không và thêm vào danh sách nếu không phải vậy.

FYI, tôi hiện đang sử dụng IE7.

+0

Tôi không nghĩ rằng bạn có thể ... Nhưng nếu bạn có thể tôi muốn xem như thế nào! – marcgg

+0

Tôi không nghĩ rằng bạn có thể, vì lý do an ninh. Có nói rằng, họ phải được lưu trữ * một nơi nào đó *. Biết Microsoft, trong sổ đăng ký ... – Powerlord

+4

@R. Bemrose, theres không có lý do an ninh để không cho phép nó. Nếu một chương trình độc hại có quyền truy cập ghi vào HKCU, điều này khó có thể làm được. Nếu một * trang web * có thể tự thêm vào các trang web đáng tin cậy, bây giờ điều đó sẽ là xấu. –

Trả lời

12

Có một cái nhìn tại this

Về cơ bản nó trông như thể tất cả các bạn phải làm là tạo ra khóa registry trong

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

sau đó một giá trị REG_DWORD có tên là "http" với giá trị == 2

+3

tôi sẽ đề nghị sử dụng tin enum TrustedZoneType { Intranet = 1, TrustedSites = 2, Internet = 3, RestrictedSites = 4 } – itsho

9

Đây là triển khai mà tôi đã đưa ra để viết các khóa registry trong .NET.

Cảm ơn bạn đã đặt tôi đi đúng hướng, 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; 
     } 
    } 
} 
+1

Tôi đã gửi một RFC và vá sơ bộ để WatiN để làm điều này một phần chức năng của WatiN.Core –

+0

Ngọt ngào. Có một nơi vạch ra cách cộng đồng có thể gửi các bản vá cho WatiN không? –

+0

Tôi chỉ cần đăng lên [email protected] với bản vá chức năng, nhưng không phát hành chủ yếu dựa trên mã bạn đã đăng ở đây và bây giờ sẽ đợi nếu có bất kỳ sự quan tâm nào để bao gồm nó. Tôi sẽ đăng lại đây nếu nó được đăng nhập. Http://sourceforge.net/mailarchive/forum.php?thread_name=4A2FD162.1020103%40gmx.net&forum_name=watin-development –

5

Rất vui khi tôi xem qua các bài đăng của bạn. Điều duy nhất tôi có thể thêm vào những đóng góp tuyệt vời là một khóa đăng ký khác được sử dụng bất cứ khi nào URI chứa địa chỉ IP tức là địa chỉ không phải là tên miền hoàn toàn đủ điều kiện.

Trong trường hợp này bạn phải sử dụng một cách tiếp cận khác:

Hãy tưởng tượng tôi muốn thêm một địa chỉ IP để các trang web đáng tin cậy: nói 10.0.1.13 và tôi không quan tâm những gì giao thức.

Trong HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Internet Settings \ ZoneMap \ Ranges, tôi tạo một khóa, ví dụ: "Range1" và bên trong tạo các giá trị sau:

DWORD có tên "*" và giá trị 0x2 (cho tất cả giao thức (*) và trang tin cậy (2)) Một chuỗi có tên ": Range" giá trị "10.0.1.13"

+0

Tôi đã sử dụng phương pháp tiếp cận khóa miền thành công với địa chỉ IP. Tôi đã thử các phương pháp tiếp cận chính phạm vi ban đầu và nó đã không hoạt động. Cụ thể về các giao thức có vẻ cũng quan trọng. –

-1

Nếu trang web có thể tự thêm vào trang web đáng tin cậy, bây giờ sẽ rất tệ.

tôi không khá agree- miễn là trình duyệt yêu cầu người dùng cho phép, khả năng của một trang web để thêm bản thân đến các trang web đáng tin cậy rất có thể đơn giản hóa trải nghiệm người dùng, nơi người dùng tin tưởng tên miền và muốn hiển thị trang chính xác.

Cách khác là người dùng phải tự chuyển sang tùy chọn internet để thêm miền, cho người dùng của tôi, không thể thực hiện được.

Tôi đang tìm một phương thức php hoặc javascript để trang web tự thêm, hoặc thông qua một số api IE hoặc thông qua sổ đăng ký như bạn đã giải thích một cách hữu ích ở trên!

đã tìm thấy những giải pháp khả thi cho đến nay:

  • php via shell
  • người khác tôi không được phép liệt kê ở đây, vì tôi không có đủ điểm
2

Ngoài adding the domain to the Trusted Sites list, bạn cũng có thể cần thay đổi cài đặt "Tự động nhắc tải xuống tệp" cho vùng Trang web đáng tin cậy. Để thực hiện điều đó theo chương trình, bạn sửa đổi chìa khóa/giá trị:

HKCU \ Software \ Microsoft \ Windows \ CurrentVersion \ Internet Settings \ Zones \ 2 @ 2200

Thay đổi giá trị từ (Tắt) thành (Bật). Dưới đây là một số mã C# để làm điều đó:

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

Đây là việc triển khai thêm trang web đáng tin cậy theo chương trình vào IE - dựa trên mã của Even Mien. Nó cũng hỗ trợ tên miền và địa chỉ IP. Giới hạn không có giao thức cụ thể có thể được xác định, thay vào đó nó chỉ đơn giản là sử dụng "*" cho tất cả các giao thức.

// 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

Sử dụng PowerShell thật dễ dàng.

#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 
Các vấn đề liên quan