【问题标题】:LDAP path for Active DirectoryActive Directory 的 LDAP 路径
【发布时间】:2019-05-30 19:41:32
【问题描述】:

我正在使用 MVC 在 ASP.net 4.0 中开发 Web 应用程序。

在我的应用程序中,我使用 Exchnage server 2007 发送电子邮件。

我正在从 Exchange 服务器获取全局地址列表。

现在的问题是如何使用用户名、密码和域名获取 Active Directory 的 LDAP 路径。

目前我正在做的是使用 DirectoryEntry 的对象 n 为我预先知道的服务器传递 LDAP 路径。

但是如果其他未知的交换服务器的凭据正在使用呢?

【问题讨论】:

  • 我不熟悉交换全局地址列表,但我做了很多活动目录工作。如果您可以分享您的代码并显示您硬编码的部分,我想我可以帮助使其独立于域

标签: asp.net-mvc-3 active-directory ldap


【解决方案1】:

由于您使用的是 .NET 3.5 及更高版本,因此您应该查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:

Managing Directory Security Principals in the .NET Framework 3.5

基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find yourself:
UserPrincipal myself = UserPrincipal.Current;

// find user by name
UserPrincipal someoneElse = UserPrincipal.FindByIdentity("John Doe");

// get user's LDAP path
if(someoneElse != null)
{
   // the DN (DistinguishedName) is typically your full LDAP path to the object
   // just prepend it with LDAP:// and you should be able to bind to it
   string userDN = someoneElse.DistinguishedName;

   // if you need the full LDAP path, you need to look at the underlying
   // DirectoryEntry object from System.DirectoryServices:
   string ldapPath = (someoneElse.GetUnderlyingObject() as DirectoryEntry).Path;
}

新的 S.DS.AM 使在 AD 中与用户和组一起玩变得非常容易。因此,如果您从 Exchange 获得一些信息,例如用户名,您应该可以很容易地在 AD 中找到对应的 UserPrincipal,然后从那里做任何您需要做的事情。

【讨论】:

    【解决方案2】:

    我不熟悉 .net。但我可以在一定程度上帮助解决问题

    我猜你正在寻找用户的 fdn

    域控制器有一个名称的 dns SRV 记录

           _ldap._tcp.<DNSDomainName>
           _ldap._tcp.example.com
    

    在这种情况下,它会告诉您运行 AD 的服务器的 fqdn。 (基本上是 ldap 服务)。假设你得到,

           host.example.com
    

    然后在主机“host.example.com”上从 dc=example,dc=com 进行子树 ldap 搜索。 会是这样,

        ldapsearch -h host.example.com -b "dc=example,dc=com" -s sub samaccountname=username
    

    这将获得用户的 ldap 路径 (ldap dn)。如果将 AD 配置为不响应任何匿名请求,还会出现另一个问题。

    但是,如果您正在寻找 .Net 解决方案,这可能没有任何意义。你可以试试上面的解决方案。

    【讨论】:

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