[英]How can I add `IIS AppPool\DefaultAppPool` to a group?
我使用以下方式将用户添加到组中:
// 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
与NT Service\\w32time
和Administrator
一起使用,它在IIS AppPool\\DefaultAppPool
也返回null。 使用lusrmgr.msc
,可以不加任何限制地添加用户,但是我不能使用UserPrincipal
, GroupPrincipal
或Principal
来获取用户。
如何获取IIS AppPool\\DefaultAppPool
的Principal
或将其添加到本地组?
您可以通过SID来回获取Principal
:
private Principal GetUserPrincipal(PrincipalContext context, string name)
{
var nta = new NTAccount(name);
try
{
var sid = nta.Translate(typeof(SecurityIdentifier)).Value;
return Principal.FindByIdentity(context, IdentityType.Sid, sid);
}
catch (IdentityNotMappedException)
{
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.