Change Application Pool Identity Programmatically

Security doesn't allow developers to have IIS installed locally on their PCs, nor do they allow developers to be in the administrator group of the development web server. In order for them to debug, we have individual application pools running under the identity of the developer. (Thankfully IIS 6.0 has allowed this feature.) We wrote a small program that allows a user to recycle their application pool:

public void RecycleAppPool(string appPoolLocation, string appPoolName)
{
   //Execute Code to recycle a user's app pool
   DirectoryEntry w3svc = new DirectoryEntry(appPoolLocation + appPoolName);
   w3svc.Invoke("Recycle",
null);
}

What I would also like to do is allow them to change their application pool's identity password when it changes every 90 days. How would I do this programmatically




Answer this question

Change Application Pool Identity Programmatically

  • Ryan B

    You might have a application pool looks like:
    IIS://Localhost/W3SVC/AppPools/myAppPoolSample

    metabasepath: IIS://Localhost/W3SVC/AppPools
    AppPoolName: myAppPoolSample
    AppPoolUser:  <your specified user>
    AppPoolPass:  <your specified user's passwd>

    Sorry that I didn't explain those parameters clearly.


  • Wang Chi

    That makes sense. I don't see the method "InvokeSet". I tried just using Invoke, but I'm getting the error: "The parameter is incorrect".

  • Mikhail Arkhipov

    string metabasePath = "IIS://PHL1S331/W3SVC/AppPools";
    string appPoolName = "hr90117";
    string appPoolUser = "hr90117";
    string appPoolPass = "test";

    DirectoryEntry myAppPool;
    DirectoryEntry apppools =
    new DirectoryEntry(metabasePath);
    myAppPool = apppools.Children.Find(appPoolName, "IIsApplicationPool");
    myAppPool.Invoke("AppPoolIdentityType",
    new Object[] { 3 });
    myAppPool.Invoke("WAMUserName",
    new Object[] { Environment.MachineName + @"\" + appPoolUser });
    myAppPool.Invoke("WAMUserPass",
    new Object[] { appPoolPass });
    myAppPool.Invoke("SetInfo",
    null);
    myAppPool.CommitChanges();



  • Kishore79

    Thanks for this. What do I do for the metabasepath

  • Tom2

    The following is a snippit of text from the Metabase.xml file.

    <
    IIsApplicationPool Location="/LM/W3SVC/AppPools/Jason" AppPoolAutoStart="TRUE" AppPoolIdentityType="3" CPUAction="1" CPULimit="0" CPUResetInterval="2" PeriodicRestartMemory="0" PeriodicRestartPrivateMemory="0" PeriodicRestartRequests="0" RapidFailProtection="TRUE" WAMUserName="use1\Jason" WAMUserPass="************" />

    Would I just use "Jason" as my appPoolName and "IIS://localhost/W3SVC/AppPools" as my metabasePath

  • Zoltan Magyar



    /*modify metabasePath, appPoolName, appPoolUser and appPoolPass */

    DirectoryEntry newpool;

    DirectoryEntry apppools = new DirectoryEntry(metabasePath);

    newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool");

    newpool.InvokeSet("AppPoolIdentityType", new Object[] { 3 });

    newpool.InvokeSet("WAMUserName", new Object[] { Environment.MachineName + @"\" + appPoolUser });

    newpool.InvokeSet("WAMUserPass", new Object[] { appPoolPass });

    newpool.Invoke("SetInfo", null);


     


  • jimg43

    Yes, please.

  • NickP

  • RickSolie

    Do you still need the answer
  • Snarf99

    Finally, I got it!

    myAppPool.Properties["WAMUserPass"][0] = appPoolPass;



  • sramshaw

    Oops, forget to call newpool.CommitChanges(); as I add a new pool. You know where to put it. If you want to modify the existing one, use the Find() instead of Add(). 
  • Change Application Pool Identity Programmatically