【问题标题】:How to find out that my Windows user is a domain user?如何确定我的 Windows 用户是域用户?
【发布时间】:2012-06-09 14:06:46
【问题描述】:

我需要确定我的 windows currentuser 是域用户还是本地用户? 我得到了我的 CurrentPrincipal :

 System.Threading.Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());

【问题讨论】:

    标签: c# active-directory


    【解决方案1】:

    Windows 非域用户通常没有 UPN 名称。如下所示如果我使用 /upn 触发 WHOAMI:

    C:\>WHOAMI /UPN
    ERROR: Unable to get User Principal Name (UPN) as the current logged-on user
           is not a domain user.
    

    基于此使用下面的代码,我们可以确定用户是否是域用户。

     var upnName = System.DirectoryServices.AccountManagement.UserPrincipal.Current.UserPrincipalName;
        if (upnName == null)
        {
            //not a domain user
        }
    

    另一种冗长的方法是获取 SAM 格式的用户名。然后使用 DirectoryEntry、DirectorySearcher 和其他 AD API 遍历机器的所有加入域,并查找用户是否在我们迭代的任何域中。

    查找机器ex1ex2的所有域

    Here is How to get domain user information from Active Directory in C#

    【讨论】:

      【解决方案2】:

      我没有可用的广告,但我希望这可能会起作用:

      public bool IsDomainUser()
      {
        bool isDomain = false; 
        foreach (var grp in WindowsIdentity.GetCurrent().Groups)
        {
            if (grp.IsValidTargetType(typeof(SecurityIdentifier)))
            {
              SecurityIdentifier si = 
                      (SecurityIdentifier)grp.Translate(typeof(SecurityIdentifier));
              // not sure if this is right, but I'm not in a domain and don't have this SID
              // I do have LocalSID however, so you might need to turn it around,
              // if you have LocalSid you are not using a domain acount
              if (si.IsWellKnown(WellKnownSidType.BuiltinDomainSid))
              {
                  isDomain = true;
              }
            }
         }
         return isDomain;
       }
      
      
      
         // for debugging list SIDS for current user and its groups
         foreach (var obj in Enum.GetValues(typeof(WellKnownSidType)))
         {
      
          if (WindowsIdentity.GetCurrent().User.IsWellKnown((WellKnownSidType)obj))
          {
              Debug.WriteLine("User:" + obj.ToString());
      
          }
      
          foreach (var grp in WindowsIdentity.GetCurrent().Groups)
          {
              if (grp.IsValidTargetType(typeof(SecurityIdentifier)))
              {
                  SecurityIdentifier si = 
                       (SecurityIdentifier) grp.Translate(typeof(SecurityIdentifier));
                  if (si.IsWellKnown((WellKnownSidType)obj))
                  {
                      Debug.WriteLine("Grp: " + grp.ToString() + " : " + obj.ToString());
                  }
              }
          }
        }
      

      【讨论】:

      • 看起来很有趣,一种新的方式。对一些事情感到好奇。上面,检查当前用户成员组的 SID 是否有域 SID。对?这意味着域用户总是有一些组成员身份?还有WindowsIdentity.GetCurrent().Group,它可以从域中获取组成员身份吗?
      • 如果我查看本地帐户的 SID,我会得到很多,但我不是这么多组的成员。我想您只能判断您是在处理域帐户还是本地帐户,仅此而已。如果您需要分配组成员身份,则需要查询域控制器。
      【解决方案3】:
      using System.DirectoryServices.AccountManagement;
      
      bool IsDomainUser()
      {
          return UserPrincipal.Current.ContextType == ContextType.Domain;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多