繁体   English   中英

如何将“ IIS AppPool \\ DefaultAppPool”添加到组中?

[英]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.FindByIdentityNT Service\\w32timeAdministrator一起使用,它在IIS AppPool\\DefaultAppPool也返回null。 使用lusrmgr.msc ,可以不加任何限制地添加用户,但是我不能使用UserPrincipalGroupPrincipalPrincipal来获取用户。

如何获取IIS AppPool\\DefaultAppPoolPrincipal或将其添加到本地组?

您可以通过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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM