【问题标题】:Get groups from Active Directory using C#使用 C# 从 Active Directory 获取组
【发布时间】:2010-11-30 15:45:56
【问题描述】:

我在通过 System.DirectoryServices 从 Active Directory 获取组时遇到问题

最初我在域中注册的计算机上启动我的应用程序,但由于它是一个活动域,我不想对 AD 进行任何写入,所以我设置了一台 Windows XP 作为计算机主机操作系统,并在虚拟机上安装了 windows server 2003。

我在机器中添加了另一个以太网端口并设置了一个交换机,1个以太网端口专用于VM,另一个端口用于主机。

在配置 IP 地址以使它们进行通信后,我将我的应用程序转移到主机上并启动它,但我得到了一个 DirectoryServicesCOMException。

提示用户名和密码无效 :( 只是为了检查它不是活动目录,我创建了第三台虚拟机并安装了 Windows XP,我使用在 APP 中测试的凭据将其添加到域中,工作一种享受。

所以我认为这一定是因为运行应用程序的机器不是域的一部分。

这是导致问题的代码块:

public CredentialValidation(String Domain, String Username, String Password, Boolean Secure)
{
     //Validate the Domain!
     try
     {
         PrincipalContext Context = new PrincipalContext(ContextType.Domain, Domain); //Throws Exception
         _IsValidDomain = true;

         //Test the user login
         _IsValidLogin = Context.ValidateCredentials(Username, Password);

         //Check the Group Admin is within this user
         //******HERE
         var Results = UserPrincipal.FindByIdentity(Context, Username).GetGroups(Context);

         foreach(Principal Result in Results)
         {
             if (Result.SamAccountName == "Domain Admins")
             {
                 _IsAdminGroup = true;
                 break;
             }
         }
         Results.Dispose();
         Context.Dispose();
     }
     catch (PrincipalServerDownException)
     {
         _IsValidDomain = false;
     }
 }

登录对话框中的信息是这样输入的:

Domain: test.internal
Username: testaccount
Password: Password01

希望有人能解释一下这个错误。


更新:

检查服务器上的安全日志后,我可以看到我的登录尝试是成功的,但这归结为:

_IsValidLogin = Context.ValidateCredentials(Username, Password);

我检查组之后的行导致错误,所以主要问题是下面的代码行在未加入网络的机器上无法正常工作: p>

var Results = UserPrincipal.FindByIdentity(Context, Username).GetGroups(Context);

【问题讨论】:

  • 您确定该方法输入的用户凭据对于您尝试对给定用户进行身份验证的给定域是正确的吗?这个用户真的存在吗?您是否尝试过使用 System.DirectoryServices 命名空间中的 DirectoryEntry 和 DirectorySearcher 类的老式方法?只是建议,我不太了解 Principal 对象。
  • 用户存在,因为我在网络上放置了另一台具有相同凭据的 VM XP 机器,我可以在 AD 中看到该用户,并通过在 cmd 中运行命令 start \\server1 我得到凭据对话框和能够通过它进行身份验证,这在属于网络的机器上 100% 有效。

标签: c# active-directory active-directory-group


【解决方案1】:

根据您的代码 sn-p,在调用 ValidateCredentials 之前尝试创建 PrincipalContext 时会失败。此时,运行您的代码的线程仍然在本地身份(如果您在 Web 进程中)或您在计算机上登录的身份(对于 Windows 进程)下工作。 test.internal 域中不存在这些中的任何一个。

您可能想尝试在构造函数中包含用户名和密码的 PrincipalContext 重载。见http://msdn.microsoft.com/en-us/library/bb341016.aspx

【讨论】:

  • 好的,我明白了,但是如果 PrincipleContext 失败,我的安全日志中的服务器日志我可以看到来自客户端 PC 的用户名成功登录,我有一种感觉将与本地 Windows 身份有关,但我尝试过这样做:TEST/testaccount 和 testaccount@test 以及 testaccount@test.internal 并且都失败了。我也确定我尝试了 Principle Context 方法的重载,我会再试一次并更新。
  • 哇,我确定它测试了这个:/,行 'PrincipalContext(ContextType.Domain, Domain);'已更新为保存用户名和密码,现在似乎工作正常。
【解决方案2】:

我过去常常通过 C# .NET 进行大量用户管理。我只是挖了一些你可以尝试的方法。

以下两种方法将为给定的 SAM 帐户名称获取 DirectoryEntry 对象。它需要一个 DirectoryEntry,它是您要开始搜索帐户的 OU 的根目录。

另一个将为您提供用户所属组的专有名称列表。然后,您可以使用这些 DN 搜索 AD 并获取 DirectoryEntry 对象。

public List<string> GetMemberOf(DirectoryEntry de)
{
  List<string> memberof = new List<string>();

  foreach (object oMember in de.Properties["memberOf"])
  {
    memberof.Add(oMember.ToString());
  }

  return memberof;
}

public DirectoryEntry GetObjectBySAM(string sam, DirectoryEntry root)
{
  using (DirectorySearcher searcher = new DirectorySearcher(root, string.Format("(sAMAccountName={0})", sam)))
  {
    SearchResult sr = searcher.FindOne();

    if (!(sr == null)) return sr.GetDirectoryEntry();
    else
      return null;
  }
}

【讨论】:

  • 这是使用我不想用于身份验证过程的目录条目,DirectoryServices 有一个用于处理的内置命名空间AccountManagement
  • 是的,我只是在谈论根据您的问题标题获取用户所属组的列表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-01
  • 2018-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多