2009-06-09 18 views
10

Sto facendo un progetto di automazione IE utilizzando WatiN.Aggiunta programmatica di siti attendibili a Internet Explorer

Quando un file da scaricare viene cliccato, ottengo il seguente nella barra informazioni di Internet Explorer:

Per facilitare la protezione, Internet Explorer ha bloccato questo sito di scaricare file a voi computer.

Per scaricare il rapporto, posso aggiungere manualmente il sito alla lista dei siti attendibili di Internet Explorer, ma io preferirei di controllare a livello di codice in .NET per vedere se il sito è attendibile e aggiungerlo alla lista se non è.

FYI, Attualmente sto utilizzando IE7.

+0

Non credo che tu possa ... Ma se puoi, mi piacerebbe vedere come! – marcgg

+0

Non penso che sia possibile, per ragioni di sicurezza. Detto questo, devono essere memorizzati * da qualche parte *. Conoscendo Microsoft, nel registro ... – Powerlord

+4

@R. Bemrose, non c'è motivo di sicurezza per non permetterlo. Se un programma dannoso ha accesso in scrittura a HKCU, questo non è certo il peggiore che potrebbe fare. Se un * sito web * potrebbe aggiungersi ai siti attendibili, ora sarebbe male. –

risposta

12

Dai un'occhiata alla this

In sostanza è come se tutto ciò che dovete fare è creare chiave di registro in

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

poi un valore REG_DWORD denominato "http" con il valore == 2

+3

Io suggerirei di usare enum TrustedZoneType privato { Intranet = 1, TrustedSites = 2, Internet = 3, RestrictedSites = 4 } – itsho

9

Ecco l'implementazione che mi è venuta in mente per scrivere le chiavi di registro in .NET.

Grazie per avermi messo nella giusta direzione, 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

ho presentato una RFC e la patch preliminare per WatiN a rendere questa parte della funzionalità WatiN.Core –

+0

Dolce. C'è un luogo che delinea il modo in cui la community può presentare patch per WatiN? –

+0

Ho semplicemente postato su [email protected] con una patch di qualità funzionale, ma non di rilascio, basata principalmente sul codice che hai postato qui e ora aspetterò se c'è qualche interesse a includerlo. Inserirò di nuovo qui se dovesse entrare. Http://sourceforge.net/mailarchive/forum.php?thread_name=4A2FD162.1020103%40gmx.net&forum_name=watin-development –

5

Contento di aver trovato i vostri messaggi. L'unica cosa che posso aggiungere agli eccellenti contributi è che viene utilizzata una chiave di registro diversa ogni volta che l'URI contiene un indirizzo IP, ad esempio l'indirizzo non è un nome di dominio completo.

In questo caso è necessario utilizzare un approccio alternativo:

Immagina desidero aggiungere un indirizzo IP per i siti attendibili: dico 10.0.1.13 e non mi importa quale protocollo.

Sotto HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Internet Settings \ ZoneMap \ Ranges, creo una chiave, ad es. "Range1" e l'interno che creano i seguenti valori:

A DWORD con nome "*" e valore 0x2 (per tutti i protocolli (*) e sito attendibile (2)) Una stringa con nome ": intervallo" con valore "10.0.1.13"

+0

Ho usato l'approccio con chiave di dominio con successo con un indirizzo IP. Ho provato inizialmente l'approccio key range e non ha funzionato. Essere specifici sui protocolli sembra essere anche importante. –

-1

Se un sito web si potrebbe aggiungere ai siti attendibili, ora che sarebbe male.

Io non abbastanza accordi finché il browser chiede all'utente il permesso, la capacità di un sito da aggiungere se stesso a siti attendibili in grado di semplificare notevolmente l'esperienza dell'utente, in cui l'utente si fida del dominio e vuole la corretta visualizzazione della pagina.

L'alternativa è che l'utente deve accedere manualmente alle opzioni Internet per aggiungere il dominio, che è, per i miei utenti, non praticabile.

sto cercando un php o JavaScript metodo per il sito da aggiungere se stessa, sia attraverso alcuni IE api, o tramite il Registro di sistema come hai così utilmente spiegato sopra!

hanno trovato queste possibili soluzioni finora:

  • php via shell
  • gli altri non mi è permesso di elencare qui perché non ho abbastanza punti
2

Oltre a adding the domain to the Trusted Sites list, potrebbe inoltre essere necessario modificare l'impostazione "Richiedi automaticamente download di file" per l'area Siti attendibili. Per farlo programmaticamente, si modifica la chiave/valore:

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

modificare il valore da (Disabilita) a (Abilita). Ecco un po 'di codice C# per farlo:

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

Ecco l'attuazione di aggiungere siti attendibili a livello di codice per IE - sulla base anche del codice di Mien. Supporta anche il nome di dominio e l'indirizzo IP. La limitazione non è un protocollo specifico potrebbe essere definito, invece utilizza semplicemente "*" per tutti i protocolli.

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

L'utilizzo di PowerShell è abbastanza semplice.

#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