views:

682

answers:

2

I am using the code below to create a new application pool in the Installer class of my application:

private static void CreateAppPool(string serverName, string appPoolName)
{
    //  metabasePath is of the form "IIS://<servername>/W3SVC/AppPools"
    //    for example "IIS://localhost/W3SVC/AppPools" 
    //  appPoolName is of the form "<name>", for example, "MyAppPool"
    string metabasePath = string.Format("IIS://{0}/W3SVC/AppPools", serverName);
    Console.WriteLine("\nCreating application pool named {0}/{1}:", metabasePath, appPoolName);
    try
    {
        DirectoryEntry apppools = new DirectoryEntry(metabasePath);
        DirectoryEntry newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool");
        newpool.CommitChanges();
        Console.WriteLine("AppPool created.");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed in CreateAppPool with the following exception: \n{0}", ex.Message);
    }
}

How can I change the user credentials under which this application pool is running?

+2  A: 

Add the following to your code just after the line where you create newpool:

DirectoryEntry newpool = 
            apppools.Children.Add(appPoolName, "IIsApplicationPool");
// Add this:
newpool.Properties["AppPoolIdentityType"].Value = 3;
newpool.Properties["WAMUserName"].Value = 
            Environment.MachineName + @"\" + username;
newpool.Properties["WAMUserPass"].Value = password;

You'll obviously need to add the string variables username and password to your CreateAppPool() method parameters as well.

Another thing you need to do, if you weren't already aware, is make sure your application pool user gets sufficient rights to access the IIS metabase, ASP.NET temp folders etc. You can do this by running the following command:

aspnet_regiis.exe -ga <username>

You can find this tool in the folder %SYSTEMROOT%\Microsoft.NET\Framework\v2.0.50727. I usually just shell out using System.Diagnostics.Process.

And finally, the application pool user will need (at least) read rights on the web folder for the app.

Kev

Kev
A: 
aspnet_regiis.exe -ga <username>

Is probably not the best command to use.

You should add the APPPool user to the IIS_WPG group and grant rights using that group.

Christopher_G_Lewis
You do need to use aspnet_regiis.exe -ga to assign proper worker process identity rights/permissions. That's what it's designed for.
Kev