我正在使用WatiN進行IE自動化項目。以編程方式將受信任的站點添加到Internet Explorer中
當點擊要下載的文件,我得到的Internet Explorer信息欄下面:
爲幫助保護您的安全, Internet Explorer已經從網上下載文件到您 阻止此 網站電腦。
爲了下載報告,我可以手動添加網站到Internet Explorer的受信任站點列表,但我寧願在.NET編程檢查,看看是否該網站是可信的,並把它添加到列表如果不是。
僅供參考,我目前正在使用IE7。
我正在使用WatiN進行IE自動化項目。以編程方式將受信任的站點添加到Internet Explorer中
當點擊要下載的文件,我得到的Internet Explorer信息欄下面:
爲幫助保護您的安全, Internet Explorer已經從網上下載文件到您 阻止此 網站電腦。
爲了下載報告,我可以手動添加網站到Internet Explorer的受信任站點列表,但我寧願在.NET編程檢查,看看是否該網站是可信的,並把它添加到列表如果不是。
僅供參考,我目前正在使用IE7。
下面是我在.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;
}
}
}
很高興我遇到您的帖子。我可以添加到優秀貢獻中的唯一事情是,只要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」
如果一個網站可以自己添加到受信任的站點,現在這將是糟糕的。
我不太品質協議,只要在瀏覽器詢問權限的用戶,一個網站將自身添加到受信任的站點可以大大簡化用戶體驗,用戶信任域的能力並希望顯示正確的頁面。
另一種方法是用戶必須手動進入Internet選項才能添加域,這對我的用戶來說是不可行的。
我正在尋找一個php或javascript方法爲網站添加自己,無論是通過一些IE瀏覽器API,或通過註冊表,因爲你已經如此有益地解釋上面!
迄今已發現了這些可能的解決方案:
除了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);
}
}
這裏是增加信任的網站編程到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);
}
}
使用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
我不認爲你可以...但如果你能我想看看怎麼樣! – marcgg 2009-06-09 20:39:24
出於安全考慮,我不認爲你可以。話雖如此,它們必須存儲在*某處*。瞭解微軟,在註冊表中... – Powerlord 2009-06-09 20:42:11
@R。 Bemrose,theres沒有安全理由不允許它。如果一個惡意程序對HKCU有寫入權限,這可能不是最糟糕的。如果*網站*可以將自己添加到受信任的站點,那麼現在這會很糟糕。 – 2009-06-09 21:00:41