1

I am trying to add an IIS Application Pool user to a local user group remotely via C# and am having some difficulty.

I have tried the two approaches below:

// This results in a ArgumentNullException because user is never set
using (PrincipalContext pc = new PrincipalContext(ContextType.Machine, serverName))
{
    UserPrincipal user = UserPrincipal.FindByIdentity(pc, String.Format(@"IIS APPPOOL\{0}", rootApplicationPoolName));
    GroupPrincipal myGroup = GroupPrincipal.FindByIdentity(pc, "mygroupname");
    myGroup.Members.Add(user);
    myGroup.Save();
}

Also:

// This results in a NoMatchingPrincipalException saying the user could not be found
using (PrincipalContext pc = new PrincipalContext(ContextType.Machine, serverName))
{
    GroupPrincipal myGroup= GroupPrincipal.FindByIdentity(pc, "mygroupname");
    myGroup.Members.Add(pc, IdentityType.Name, String.Format(@"IIS APPPOOL\{0}", appPoolName));
    myGroup.Save();
}

I can manually add this user to the group and it works just fine.

What am I missing?

mynameiscoffey
  • 15,244
  • 5
  • 33
  • 45

0 Answers0