LDAP路径问题

时间:2011-10-16 20:40:35

标签: c# asp.net authentication active-directory ldap

我正在使用LDAP,我是新手。

当您只知道用户名,密码,服务器名

时,是否有办法获取域名?

我正在尝试这样做:

string ldapPath = "LDAP://serverName";
string uid = username;
string password = pwd;
string qry = String.Format("(uid={0})", uid);
string adsPath = String.Empty;

try
{
    DirectoryEntry nRoot = new DirectoryEntry(ldapPath, null, null, AuthenticationTypes.Anonymous);

    DirectorySearcher ds = new DirectorySearcher(nRoot, qry);
    SearchResult sr = ds.FindOne();

    if (sr != null)
    {
       // we want to retrieve the DN like this: "uid=myuser,ou=People,dc=findlay,dc=edu
       ldapPath = sr.Path; //update where we will bind next
    }

除非我改变

,否则这不起作用
 string ldapPath = "LDAP://serverName";

 string ldapPath = "LDAP://serverName/DC=mydomain,DC=com";

任何帮助.. ??

由于

编辑rootDSE

string defaultNamingContext;

using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://serverName/rootDSE", null, null, AuthenticationTypes.Anonymous))
{
    defaultNamingContext = rootDSE.Properties["rootDomainNamingContext"].Value.ToString();
}

我也觉得这是解决方案,但它目前对我不起作用..请帮忙!

4 个答案:

答案 0 :(得分:1)

你可以这样尝试

//方法调用

string netBiosName = GetNetBiosName(LDAP://CN=Partitions,CN=Configuration,DC=<DomainName>,DC=<local|com>,  "<userName"", "<password>");

//方法调用

//方法定义

private string GetNetBiosName(string ldapUrl, string userName, string password)
{
   string netbiosName = string.Empty;
  DirectoryEntry dirEntry = new DirectoryEntry(ldapUrl,userName, password);

   DirectorySearcher searcher = new DirectorySearcher(dirEntry);
   searcher.Filter = "netbiosname=*";
   searcher.PropertiesToLoad.Add("cn");

   SearchResultCollection results = searcher.FindAll();
   if (results.Count > 0)
   {
    ResultPropertyValueCollection rpvc = results[0].Properties["CN"];
    netbiosName = rpvc[0].ToString();
   }
   return netbiosName;

}

请查看此链接for more info

答案 1 :(得分:1)

您应该只需调用RootDse即可获取域名。

答案 2 :(得分:1)

RootDSE不受服务器限制 - 试试这个:

string defaultNamingContext;

using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://rootDSE", null, null, AuthenticationTypes.Anonymous))
{
    defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value.ToString();
}

或者,如果您使用的是.NET 3.5及更高版本,则可以使用PrincipalContext代替,可以在没有任何路径的情况下构建它 - 它只会选择您连接到的默认域:

PrincipalContext context = new PrincipalContext(ContextType.Domain);

您应该查看System.DirectoryServices.AccountManagement(S.DS.AM)命名空间。在这里阅读所有相关内容(即.NET 3.5及更新版本):

答案 3 :(得分:1)

如果:

using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://serverName/DC=mydomain,DC=com") 
{ 
    ... 
} 

有效,你试试(不是匿名):

string defaultNamingContext; 

using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://serverName/rootDSE") 
{ 
    defaultNamingContext = rootDSE.Properties["rootDomainNamingContext"].Value.ToString(); 
}

using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://serverName/rootDSE", user, password) 
{ 
    defaultNamingContext = rootDSE.Properties["rootDomainNamingContext"].Value.ToString(); 
}

它适用于我,来自不在域中的计算机。

相关问题