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?
Add the following to your code just after the line where you create
newpool:You’ll obviously need to add the string variables
usernameandpasswordto yourCreateAppPool()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:
You can find this tool in the folder
%SYSTEMROOT%\Microsoft.NET\Framework\v2.0.50727. I usually just shell out usingSystem.Diagnostics.Process.And finally, the application pool user will need (at least) read rights on the web folder for the app.