AD中如何获取当前登录用用户的OU组
现在就是登录时候验证了AD用户,但是只是验证AD中存在该用户,但是拿不到登录用户的所属OU组。看到一个代码:
public string GetOU(string domainADsPath, string username, string password, string schemaClassNameToSearch)
{
string result = string.Empty;
using (DirectoryEntry entry = new DirectoryEntry(domainADsPath, username, password, AuthenticationTypes.Secure))
using (DirectorySearcher searcher = new DirectorySearcher(entry, "objectClass=organizationalUnit"))
{
PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain, domainADsPath, username, password);
UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, username);
if (user != null)
{
DirectoryEntry directoryEntry = (user.GetUnderlyingObject() as DirectoryEntry);
if (directoryEntry != null)
{
string[] directoryEntryPath = directoryEntry.Path.Split(',');
foreach (var splitedPath in directoryEntryPath)
{
string[] eleiments = splitedPath.Split('=');
if (eleiments[0].Trim() == "OU")
{
result = username + "-" + eleiments[1].Trim();
break;
}
}
}
}
}
return result;
}
可是我用这种代码,运行到 PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain, domainADsPath, username, password);
提示报错,无法与服务器取得联系。
how can i do
很急很急的!!
求大家指导,各种意见都可以。随便说说帮顶都行! --------------------编程问答-------------------- 找不到服务器啊?
检查domainADsPath是否正确先
--------------------编程问答--------------------
正确啊,我都用domainADsPath验证了用户登录,现在已经可以登录了,但是我想读取登录用户的OU组来判断登录用户的权限。 --------------------编程问答-------------------- 试下关闭防火墙或开放端口389 --------------------编程问答-------------------- 把参数里的username改成domainADsPath+ @"\" + username试试。
--------------------编程问答--------------------
这个试过了的 还是不行 --------------------编程问答--------------------
PrincipalContext的连接有关系吗?我已经登录了,只是想获取登录用户的所属OU组,用PrincipalContextPrincipalContext 做 AD 验证,提示无法访问。。
389端口是什么作用呢?
补充:.NET技术 , ASP.NET