【问题标题】:How can I determine if a Windows Identity corresponds to a local or a domain user?如何确定 Windows 标识对应于本地用户还是域用户?
【发布时间】:2014-10-02 21:28:32
【问题描述】:

我有一个 WindowsIdentity,它对应于经过身份验证的用户。如何确定身份对应的是机器上的本地用户、已添加到机器的域用户还是未添加到机器的域?

假设我有 3 个用户帐户:

  • DomainUser(域用户组的成员,未添加到任何本地组)
  • Lo​​calUser(在机器上创建的本地用户)
  • MappedDomainUser(已添加到本机组的域用户)

如何区分

  • DomainUser 和 LocalUsers
  • Lo​​calUser 和 MappedDomainUser
  • DomainUser 和 MappedDomainUser

到目前为止,我依赖于用户名并检查它是否以机器名开头。然后,我通过检查用户所属的组(如果它是所有域用户的一部分)来进一步区分。我确定这不是最好的方法。

由于我有来自 WindowsIdentity.User 属性的用户 sid,我可以以某种方式使用它吗?

【问题讨论】:

  • 检查用户名是否以机器名开头是好的,并且有效。您还可以检查该用户是否存在于域中并比较 sid 以查看它是否是同一用户。
  • 我想知道是否可以使用众所周知的 sid 类型并查看该用户也属于哪些 sid,但是 WindowsIdentity.User 似乎与任何众所周知的 sid 都不匹配。
  • 目前还不清楚您要在这里实现什么。首先,如果域中的帐户不是域用户组的成员,你还想把它算作域帐户吗?其次,为什么要区分属于任何本地组成员的域帐户和不属于任何本地组成员的域帐户? (通常您只需要检查某个特定组的成员资格。)第三,您要包括嵌套组成员资格吗?

标签: c# windows


【解决方案1】:

不确定映射的域管理员。 我只是检查用户登录的域的本地和域管理员。 不要访问诸如“builtin\Admin”之类的字符串,它们因操作系统语言版本而异。

我喜欢使用 .net 4.5 Principals 方法。 如果你可以使用 4.5,你可以做类似的事情

所以关于问题 如何区分

  • DomainUser 和 LocalUsers
  • Lo​​calUser 和 MappedDomainUser
  • DomainUser 和 MappedDomainUser

示例代码

using System;
using System.DirectoryServices.ActiveDirectory;
using System.Security.Principal
namespace xxxxx
  {
  public class UserEnvTools
     {

    public static bool IsDomainAdmin()
    {   //returns TRUE for a machine that is on a workgroup So consider GetDomain methods based on scenario 
        if (WindowsIdentity.GetCurrent().User.AccountDomainSid == null)
            return false;
        var domainAdmins = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid,
                                                  WindowsIdentity.GetCurrent().User.AccountDomainSid);
        var prin = new WindowsPrincipal(WindowsIdentity.GetCurrent());
        return prin != null && (prin.IsInRole(domainAdmins));
    }
    public static bool IsDomainUser()
    {
        //returns TRUE for a machine that is on a workgroup So consider GetDomain methods based on scenario 
        if (WindowsIdentity.GetCurrent().User.AccountDomainSid == null)
            return false;

        var domainUsers = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid,
                                                WindowsIdentity.GetCurrent().User.AccountDomainSid);
        var prin = new WindowsPrincipal(WindowsIdentity.GetCurrent());
        return prin != null && (prin.IsInRole(domainUsers));
    }

public static bool IsLocalAdmin()
{
var localAdmins = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
var prin = new WindowsPrincipal(WindowsIdentity.GetCurrent());
return prin != null && (prin.IsInRole(localAdmins));
}
    public static bool IsLocalUser()
    {
        var localUsers = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
        var prin = new WindowsPrincipal(WindowsIdentity.GetCurrent());
        return prin != null && (prin.IsInRole(localUsers));

    }
    // Current security context applies  
    public static Domain GetCurrentUserDomain()
    {
        try
        {
            return System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain();
        }
        // It may be better not to ctach such errors?
        catch (ActiveDirectoryOperationException) // no Controller/AD Forest can not be contacted
        {return null;}
        catch (ActiveDirectoryObjectNotFoundException) // The USers Domain is not known to the controller
        {return null;}
    }

    public static Domain GetCurrentMachineDomain()
    {
        try
        {
            return System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain();
        }
        // It may be better not to ctach such errors?
        catch (ActiveDirectoryOperationException) // no controller or machine is not on a domain
        { return null; }
        catch (ActiveDirectoryObjectNotFoundException) // controller found, but the machine is not known
        { return null; }
    }

【讨论】:

  • 这样更清楚吗?我仍然不确定映射用户的含义。经过身份验证的上下文在本地或在当前域/AD 中进行身份验证。我在文档中找不到对映射用户的引用。 msdn.microsoft.com/en-us/library/wwzcae1f.aspx
  • “映射用户”不是 Microsoft 术语,但问题将其定义为“作为本地组成员的域用户”。 (这个定义实际上有点模棱两可,因为它没有说明是否包含嵌套组成员资格。)
  • 关于NETWORK_SERVICE,LOCAL SERVICE,LocalSystem,IIS APPPOOLstackoverflow.com/questions/5729264/… ###### WindowsIdentity NT AUTHORITY\NETWORK SERVICE UserDomainName: MYDOMAIN UserName: MYMACHINENAME$ ###### WindowsIdentity NT AUTHORITY\LOCAL SERVICE UserDomainName: NT AUTHORITY UserName: LOCAL SERVICE ###### WindowsIdentity NT AUTHORITY\SYSTEM UserDomainName: MYDOMAIN UserName: SYSTEM ###### WindowsIdentity IIS APPPOOL\MyCustomAppPool UserDomainName: IIS APPPOOL UserName: MyCustomAppPool
【解决方案2】:

假设WindowsIdentity.NameEnvironment.UserDomainName 一样工作,如果用户名以机器名开头,那么它不在域中,否则在域中。这允许您编写

public static bool IsDomain(WindowsIdentity identity)
{
    string prefix = identity.Name.Split('\\')[0];
    if (prefix != Environment.MachineName)
        return true;
    else
        return false;
}

UserDomainName 属性首先尝试获取域名 当前用户的 Windows 帐户名的组成部分。如果说 尝试失败,此属性尝试获取域名 与 UserName 属性提供的用户名相关联。如果 该尝试失败,因为主机计算机未加入 域,然后返回主机名。

您还可以针对计算机名称和域名相同的边缘情况过滤可用域列表(例如存储在数据库中)。

【讨论】:

  • NETWORK_SERVICE,LOCAL SERVICE,LocalSystem,IIS APPPOOLstackoverflow.com/questions/5729264/… ###### WindowsIdentity NT AUTHORITY\NETWORK SERVICE UserDomainName: MYDOMAIN UserName: MYMACHINENAME$ ###### WindowsIdentity NT AUTHORITY\LOCAL SERVICE UserDomainName: NT AUTHORITY UserName: LOCAL SERVICE ##### WindowsIdentity NT AUTHORITY\SYSTEM UserDomainName: MYDOMAIN UserName: SYSTEM ###### WindowsIdentity IIS APPPOOL\MyCustomAppPool UserDomainName: IIS APPPOOL UserName: MyCustomAppPool
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-03
  • 2011-10-18
  • 2011-01-01
  • 2020-03-14
  • 1970-01-01
  • 2015-05-24
相关资源
最近更新 更多