Click here to Skip to main content
15,878,959 members
Articles / Web Development / ASP.NET

IIS security settings and different permission using installer class with custom action

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
14 Aug 2012CPOL3 min read 28.9K   581   11  
Setting IIS security type and user, accounts permissions using installer class and custom action
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.IO;
using System.Diagnostics;
using Microsoft.Web.Administration;
using Microsoft.Web.Management;
using System.Configuration;
using System.Security.AccessControl;
using System.IO;

namespace IISsettings
{
    [RunInstaller(true)]
    public partial class InstallerIISsettings : System.Configuration.Install.Installer
    {
        public InstallerIISsettings()
        {
            InitializeComponent();
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Install(IDictionary stateSaver)
        {

            base.Install(stateSaver);

            stateSaver.Add("targetvdir", Context.Parameters["targetvdir"].ToString());
           
            configureIIS(Context.Parameters["targetvdir"].ToString());
            if (!EventLog.SourceExists("SampleApplication"))
            {
                EventSourceCreationData mySource = new EventSourceCreationData("SampleApplication", "SampleApplicationLogs");
                EventLog.CreateEventSource(mySource);
                EventLog.WriteEntry("SampleApplication", "SampleApplication IIS Settings done.");
                EventLog.WriteEntry("SampleApplication", "targetvdir..." + Context.Parameters["targetvdir"].ToString());
            }
            else
            {
                EventLog.WriteEntry("SampleApplication", "SampleApplication IIS Settings done.");
                EventLog.WriteEntry("SampleApplication", "targetvdir..." + Context.Parameters["targetvdir"].ToString());
            }

            stateSaver.Add("targetdir", Context.Parameters["targetdir"].ToString());
            DirectorySecurity dirSec = Directory.GetAccessControl(@Context.Parameters["targetdir"].ToString());
            FileSystemAccessRule fsar = new FileSystemAccessRule("Everyone",
                                                                FileSystemRights.FullControl,
                                                                InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                                                PropagationFlags.None,
                                                                AccessControlType.Allow);
            dirSec.AddAccessRule(fsar);
            FileSystemAccessRule fNet = new FileSystemAccessRule("NETWORK",
                                                                  FileSystemRights.FullControl,
                                                                  InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                                                  PropagationFlags.None,
                                                                  AccessControlType.Allow);

            dirSec.AddAccessRule(fNet);
            FileSystemAccessRule fNetServ = new FileSystemAccessRule("NETWORK SERVICE",
                                                                FileSystemRights.FullControl,
                                                                InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                                                PropagationFlags.None,
                                                                AccessControlType.Allow);
            dirSec.AddAccessRule(fNetServ);
            Directory.SetAccessControl(@Context.Parameters["targetdir"].ToString(), dirSec);


        }

        /// <summary>
        /// This method change the security setting of iis for particular web application name specified during the setup 
        /// </summary>
        /// <param name="vdName"></param>
        private void configureIIS(string vdName)
        {
            using (ServerManager serverManager = new ServerManager())
            {
                Microsoft.Web.Administration.Configuration config = serverManager.GetApplicationHostConfiguration();
                Microsoft.Web.Administration.ConfigurationSection anonymousAuthenticationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication", "Default Web Site/" + vdName);
                anonymousAuthenticationSection["enabled"] = false;
                Microsoft.Web.Administration.ConfigurationSection windowsAuthenticationSection = config.GetSection("system.webServer/security/authentication/windowsAuthentication", "Default Web Site/" + vdName);
                windowsAuthenticationSection["enabled"] = true;
                serverManager.CommitChanges();
            }
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
            savedState.Add("targetdir", Context.Parameters["targetdir"].ToString());
            DirectorySecurity dirSec = Directory.GetAccessControl(("@" + Context.Parameters["targetdir"].ToString()));
            FileSystemAccessRule fsar = new FileSystemAccessRule(@"NETWORK SERVICE",
                                                                FileSystemRights.FullControl,
                                                                InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                                                                PropagationFlags.None,
                                                                AccessControlType.Allow);
            dirSec.AddAccessRule(fsar);
            Directory.SetAccessControl("@" + Context.Parameters["targetdir"].ToString(), dirSec);


        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
I am an engnineer and am presently into web soltuions. I enjoy being techo functional, I work on ASP.NET/SQL Server and at times MS-Sharepoint. I enjoy taking up complex functional assignments that need technical solutions.

Comments and Discussions