public static void AddUrlToLocalIntranet(string url)
        {
            uint zone;
            var  manager = InternetSecurityManager.CreateObject();
            int  result;

            result = manager.MapUrlToZone(url, out zone, (uint)InternetSecurityManager.SZM_FLAGS.SZM_CREATE);
            InternetSecurityManager.WinError.TestHResult(result);
            if (zone != (int)InternetSecurityManager.URLZONE.URLZONE_INTRANET)
            {
                result = manager.SetZoneMapping(zone, url, InternetSecurityManager.SZM_FLAGS.SZM_DELETE);
                InternetSecurityManager.WinError.TestHResult(result);
                result = manager.SetZoneMapping(InternetSecurityManager.URLZONE.URLZONE_INTRANET, url, InternetSecurityManager.SZM_FLAGS.SZM_CREATE);
                InternetSecurityManager.WinError.TestHResult(result);
            }
        }
        public static ActionResult DeployWebsite(Session session, Dictionary <string, WebInstallationOptions> allOptions)
        {
            RunWebAction(session,
                         featureName =>
            {
                try
                {
                    EnsureIISComponents();
                    var options             = allOptions[featureName];
                    var webInstallationInfo = WebInstallationInfo.CreateFromFeature(session, featureName);
                    bool exists             = webInstallationInfo.TargetEntryExists;
                    int siteID = webInstallationInfo.GetSiteId();
                    int scriptMapVersion;

                    RunRegIIS("-ir", options.FrameworkVersion);

                    if (webInstallationInfo.CreateAsWebsite)
                    {
                        WebsiteInstallationInfo websiteInstallationInfo = webInstallationInfo as WebsiteInstallationInfo;
                        using (DirectoryEntry machineRoot = new DirectoryEntry(WebInstallationInfo.ServerRoot))
                        {
                            string appPoolName = "";
                            if (DirectoryEntry.Exists(WebsiteInstallationInfo.AppPoolsRoot))
                            {
                                using (DirectoryEntry appPools = new DirectoryEntry(WebsiteInstallationInfo.AppPoolsRoot))
                                {
                                    appPoolName        = webInstallationInfo.WebsiteName;
                                    bool appPoolExixts = DirectoryEntry.Exists(websiteInstallationInfo.AppPoolWmiPath);
                                    using (DirectoryEntry appPool = appPoolExixts ? new DirectoryEntry(appPools.Path + "/" + websiteInstallationInfo.WebsiteName) : appPools.Invoke("Create", "IIsApplicationPool", websiteInstallationInfo.WebsiteName) as DirectoryEntry)
                                    {
                                        appPool.InvokeSet("AppPoolIdentityType", 2);    //LocalSystem 0; LocalService 1; NetworkService 2;  Custom (user & pwd) 3;  ApplicationPoolIdentity 4
                                        appPool.InvokeSet("AppPoolAutoStart", true);
                                        if (GetIisMajorVersion() > 6)
                                        {
                                            appPool.InvokeSet("ManagedRuntimeVersion", string.Format("v{0}", options.FrameworkVersion.ToString(2)));
                                            appPool.InvokeSet("ManagedPipelineMode", 0);     // Integrated 0; Classic 1
                                        }
                                        appPool.CommitChanges();
                                    }
                                }
                            }
                            using (DirectoryEntry site = exists ? new DirectoryEntry(machineRoot.Path + "/" + siteID) : machineRoot.Invoke("Create", "IIsWebServer", siteID) as System.DirectoryServices.DirectoryEntry)
                            {
                                site.Invoke("Put", "ServerComment", websiteInstallationInfo.WebsiteName);
                                site.Invoke("Put", "KeyType", "IIsWebServer");
                                site.Invoke("Put", "ServerBindings", "*:80:" + websiteInstallationInfo.WebsiteName);
                                site.Invoke("Put", "ServerState", 2);
                                site.Invoke("Put", "FrontPageWeb", 1);
                                site.Invoke("Put", "DefaultDoc", "Default.aspx");
                                site.Invoke("Put", "SecureBindings", "*:443:" + websiteInstallationInfo.WebsiteName);
                                site.Invoke("Put", "ServerAutoStart", 1);
                                site.Invoke("Put", "ServerSize", 1);
                                site.Invoke("SetInfo");
                                site.CommitChanges();

                                WebInstallationInfo.AddFeatureDetails(session, webInstallationInfo);
                                string siteRootPath = site.Path + "/Root";
                                using (DirectoryEntry siteRoot = DirectoryEntry.Exists(siteRootPath) ? new DirectoryEntry(siteRootPath) : site.Children.Add("Root", "IIsWebVirtualDir"))
                                {
                                    siteRoot.InvokeSet("Path", websiteInstallationInfo.GetWebFolderPath(options));
                                    siteRoot.InvokeSet("DefaultDoc", "Default.aspx");
                                    if (!string.IsNullOrEmpty(appPoolName))
                                    {
                                        siteRoot.InvokeSet("AppPoolId", appPoolName);
                                    }
                                    siteRoot.Invoke("AppCreate", true);
                                    siteRoot.CommitChanges();
                                    scriptMapVersion = GetScriptMapsVersion(siteRoot);
                                }
                            }
                        }
                    }
                    else
                    {
                        VirtualDirectoryInstallationInfo virtualDirectoryInstallationInfo = webInstallationInfo as VirtualDirectoryInstallationInfo;
                        using (DirectoryEntry websiteEntry = new DirectoryEntry(virtualDirectoryInstallationInfo.WebsitePath))
                        {
                            using (DirectoryEntry applicationEntry = exists ? new DirectoryEntry(virtualDirectoryInstallationInfo.TargetWmiPath) : websiteEntry.Invoke("Create", "IIsWebVirtualDir", virtualDirectoryInstallationInfo.VirtualDirectoryName) as DirectoryEntry)
                            {
                                applicationEntry.InvokeSet("Path", virtualDirectoryInstallationInfo.GetWebFolderPath(options));
                                applicationEntry.Invoke("AppCreate", true);
                                applicationEntry.InvokeSet("AppFriendlyName", virtualDirectoryInstallationInfo.VirtualDirectoryName);
                                applicationEntry.InvokeSet("DefaultDoc", "Default.aspx");
                                applicationEntry.CommitChanges();
                                WebInstallationInfo.AddFeatureDetails(session, virtualDirectoryInstallationInfo);
                            }
                            scriptMapVersion = GetScriptMapsVersion(websiteEntry);
                        }
                    }

                    // Set ASP.NET to target framework version
                    if (scriptMapVersion != options.FrameworkVersion.Major)
                    {
                        RunRegIIS(string.Format("-s {0}", webInstallationInfo.TargetWmiSubPath), options.FrameworkVersion);
                    }

                    // Mark as done
                    WebInstallationInfo.AddFeatureDetails(session, webInstallationInfo);

                    GrantConfigEncryptionAccess(options.FrameworkVersion);

                    var configFilePath  = webInstallationInfo.GetWebConfigFilePath(options);
                    var appRelativePath = webInstallationInfo.GetAppRelativePath();

                    // Data provider keys
                    EncryptConfigSections(configFilePath, siteID.ToString(), appRelativePath, options.ConfigSectionGroupEncryptionOptions, options.FrameworkVersion);

                    // Logging flat file access
                    if (options.InitializeFlatFileLogging)
                    {
                        InitializeFlatFileLogging(configFilePath);
                    }

                    // Set and encrypt connection strings and enqueue their encryption
                    SetConnectionStrings(configFilePath, options.ConnectionStrings);
                    if (options.EncryptConnectionStrings)
                    {
                        EncryptConnectionStrings(siteID.ToString(), appRelativePath, options.FrameworkVersion);
                    }

                    // Add url to internet explorer trusted zone
                    var internetSecurityManager = InternetSecurityManager.CreateObject();
                    foreach (var url in webInstallationInfo.CalculateWebUrls())
                    {
                        AddUrlToLocalIntranet(url);
                    }
                }
                catch (InstallException ex)
                {
                    throw ex;
                }
                catch (Exception ex)
                {
                    throw new InstallException(Messages.AnErrorHasOccurredAndInstallationWillBeCancelled, ex);
                }
            });
            return(ActionResult.Success);
        }