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");

  }

}

Advertisements