【发布时间】:2015-08-13 03:23:10
【问题描述】:
我正在使用以下方式将用户添加到组中:
// reference System.DirectoryServices.AccountManagement, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
using (var context = new PrincipalContext(ContextType.Machine))
using (var group = GroupPrincipal.FindByIdentity(context, GroupName))
using (var user = UserPrincipal.FindByIdentity(context, Username))
{
if (group == null || user == null)
{
throw new InvalidOperationException(string.Format("User '{0}' or group '{1}' does not exist", Username, GroupName));
}
if (!group.Members.Contains(user))
{
group.Members.Add(user);
group.Save();
}
}
但UserPrincipal.FindByIdentity 与IIS AppPool\DefaultAppPool 一起返回null,即使它与NT Service\w32time 和Administrator 一起使用。使用lusrmgr.msc,可以不添加用户,但是我不能使用UserPrincipal,GroupPrincipal,或者Principal来获取用户。
如何为IIS AppPool\DefaultAppPool 获取Principal 或以其他方式将其添加到本地组?
【问题讨论】: