1

我正在尝试通过 C# 远程将 IIS 应用程序池用户添加到本地用户组,​​但遇到了一些困难。

我尝试了以下两种方法:

// 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();
}

还:

// 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();
}

我可以手动将此用户添加到组中,它工作得很好。

我错过了什么?

4

0 回答 0