Set default domain for website programitacally using C#

Inorder to set the default domain for a website in case of Basic Authentication:

 

//set the default domain
DirectoryEntry  site = new DirectoryEntry("IIS://localhost/W3SVC/" + newWebSiteID + "/ROOT");
 site.Invoke("Put", new object[] { "DefaultLogonDomain", "CodeLeacher"});
 site.Invoke("SetInfo");
site.CommitChanges();

Advertisements

IIS Enums

Here are all the enum values needed for the IIS6 development:

public enum AccessPermissions

{

    Read = 1,

    Write = 2,

    Execute = 4,

    Source = 16,

    Script = 512,

    NoRemoteRead = 4096,

    NoRemoteWrite = 1024,

    NoRemoteExecute = 8192,

    NoRemoteScript = 16384,

    NoPhysicalDir = 32768

}

public enum SiteStatus

{

    Starting = 1,

    Started = 2,

    Stopping = 3,

    Stopped = 4,

    Pausing = 5,

    Paused = 6,

    Continuing = 7

}

public enum AppPoolStatus

{

    Starting = 1,

    Started = 2,

    Stopping = 3,

    Stopped = 4

}

public enum AuthenticationTypes

{

    Anonymous = 1,

    Basic = 2,

    Integrated = 4,

    Digest = 16,

    Passport = 64

}

public enum AppPoolIdentity

{

    LocalSystem = 0,

    LocalService = 1,

    NetworkService = 2,

    UserDefined = 3

}

public enum ASPNET

{

    v1_1_4322 = 1,

    v2_0_50727 = 2

}

Creating FTP site on IIS6 programitacally using C#

Inorder to create FTP site on IIS6

        public void CreateFTPSite(string serverName, string siteName, string siteID)

        {

            try

            {   

                //Create website on IIS

                DirectoryEntry root = new DirectoryEntry(“IIS://” + serverName + “/msftpsvc”);

                DirectoryEntry newSite = root.Children.Add(siteID, “IIsFTPServer”);

                newSite.Properties[“ServerComment”][0] = “codeleacher.com”;

                newSite.CommitChanges();

                root.CommitChanges();

 

                SetSingleProperty(“IIS://” + serverName + “/msftpsvc/”+siteID.ToString(), “ServerBindings”,    “:2122:”);

 

        private static void SetSingleProperty(string metabasePath, string propertyName, object newValue)

        {

            try

            {

                DirectoryEntry path = new DirectoryEntry(metabasePath);

                PropertyValueCollection propValues = path.Properties[propertyName];

                if (null == propValues.Value)

                    propValues.Value = “foo”;

 

                string oldType = propValues.Value.GetType().ToString();

                string newType = newValue.GetType().ToString();

                if (newType == oldType)

                {

                    path.Properties[propertyName][0] = newValue;

                    path.CommitChanges();

                }

                else

                    Console.WriteLine(” Failed in SetSingleProperty; type of new value does not match property”);

            }

            catch (Exception ex)

            {

                if (“HRESULT 0x80005006” == ex.Message)

                    Console.WriteLine(” Property {0} does not exist at {1}”, propertyName, metabasePath);

                else

                    Console.WriteLine(“Failed in SetSingleProperty with the following exception: \n{0}”, ex.Message);

            }

        }

            }

            catch (Exception ex)

            {

                throw new Exception(“Error”);

            }

        }

Add users to the IIS_WPG group using C#.Net

The following function adds a user to the IIS_WPG group using the ntrights.exe utility by Microsoft. Note that in order for this function to work you have to have the ntrights utility downloaded. To download it check Microsoft’s wensite:

http://www.microsoft.com/downloads/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en

public static void AddUserToWPG(string currentFolderPath, string user)
{
  try
  {
    Process process = new Process();
    //run the script
    process.StartInfo.FileName = System.Environment.SystemDirectory + @"\net.exe";
    process.StartInfo.Arguments = string.Format(" localgroup \"{0}\" {1} /add",     wpgGroup, user);
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.UseShellExecute = true;
    process.Start();
    process.WaitForExit();

    process.StartInfo.FileName = Path.Combine(currentFolderPath, "ntrights.exe");
    process.StartInfo.Arguments = string.Format(" +r \"seTcbPrivilege\" -u \"{0}\"",     user);
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.UseShellExecute = true;
    process.Start();
    process.WaitForExit();
  }
  catch
  {
     throw new Exception("your message");

  }

}

Change the ASP.Net version of a website on IIS6 using C#.Net

Sometimes, while creating a website pragmatically, you wish to change the ASP.Net version the site is using. The following function change the ASP.Net version a website is using to ASP.Net 2.0

public static void UpdateASPNetVersion(string siteidentifier, string virtualDirectoryName)
{
   //get virtual directory and site name for m context parameters
   string targetSite = "W3SVC/" + siteidentifier + "/ROOT";
   string targetVDir = virtualDirectoryName;
   Process process = new Process();
   //run the script
   process.StartInfo.FileName =         Path.GetDirectoryName(System.Environment.SystemDirectory) + @"\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe";
   process.StartInfo.Arguments = string.Format(" -s {0}/{1}", targetSite, targetVDir);
   process.StartInfo.CreateNoWindow = false;
   process.Start();
   process.WaitForExit();
}

You can of course change the ASP.Net version to any other version by choosing a different directory.

Check if a TCP port is already in use by IIS6 using C#.Net

The following function checks if a port is already in use by the IIS. Such function can be used to validate ports before creating a website. The function simply attempts to open a connection to the specified port. In case there was an exception, that means that it is not used.


public static bool IsPortUsed(int port)
{
   try
   {
      TcpListener tcp = new TcpListener(port);
      tcp.Start();
      tcp.Stop();
      return true;
   }
   catch (SocketException)
   {
      return false;
   }
}

Get a Website Identifier from IIS6 using C#.Net

This function retrieves a website identifier from the IIS. Sometimes, you need a website identifier in order to connect to it and install something under it pragmatically. This function simply connects to the IIS and retrieve all the sub directories and then start searching for the desired website by its name and then return its identifier.


public static int GetWebSiteIdentifier(string server, string siteName)
{
   int siteIdentifier = 0;
   try
   {
     DirectoryEntry root = new DirectoryEntry("IIS://" + server + "/W3SVC");
     foreach (DirectoryEntry entry in root.Children)
     {
       if (string.Compare(entry.SchemaClassName, "IIsWebServer", true) == 0 &&      string.Compare(entry.Properties["ServerComment"].Value.ToString(), siteName, true) == 0)
       siteIdentifier = Int32.Parse(entry.Name);
     }
  }
  catch
  {
    throw new Exception("your message");
  }

  return siteIdentifier;

}