【问题标题】:How can I add `IIS AppPool\DefaultAppPool` to a group?如何将 `IIS AppPool\DefaultAppPool` 添加到组?
【发布时间】: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.FindByIdentityIIS AppPool\DefaultAppPool 一起返回null,即使它与NT Service\w32timeAdministrator 一起使用。使用lusrmgr.msc,可以不添加用户,但是我不能使用UserPrincipalGroupPrincipal,或者Principal来获取用户。

如何为IIS AppPool\DefaultAppPool 获取Principal 或以其他方式将其添加到本地组?

【问题讨论】:

    标签: c# windows


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 2014-03-13
      相关资源
      最近更新 更多