LDAP 路径问题

发布于 2024-12-10 12:18:43 字数 1197 浏览 0 评论 0 原文

我正在使用 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();
}

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

I am working with LDAP and I am new to this.

Is there a way to get the domain when you only know the username, password, servername

I am trying to do this:

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
    }

This does not work unless I change

 string ldapPath = "LDAP://serverName";

to

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

Any help..??

Thanks

Edit rootDSE

string defaultNamingContext;

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

I too feel this is the solution but it is currently not working for me.. please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

霊感 2024-12-17 12:18:43

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 及更高版本):

RootDSE is not server-bound - try this:

string defaultNamingContext;

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

Or if you're on .NET 3.5 and newer, you could use PrincipalContext instead, which can be constructed without any path - it will just pick up the default domain you're connected to:

PrincipalContext context = new PrincipalContext(ContextType.Domain);

You should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here (that's .NET 3.5 and newer):

忆梦 2024-12-17 12:18:43

如果:

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(); 
}

它对我有效,来自不在域中的计算机。

If :

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

works, have you try (without being anonymous):

string defaultNamingContext; 

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

or

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

It works for me, from a computer not in the domain.

土豪 2024-12-17 12:18:43

你可以这样尝试

// 方法调用

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;

}

请查看此链接 了解更多信息

you can try like this

// Method call

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

// Method call

// Method Definition

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;

}

pls take a look at this link for more info

花落人断肠 2024-12-17 12:18:43

您应该能够通过调用 RootDse 来获取域。

You should be able to get the domain by just calling RootDse.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文