首页 / C# / c#-使用LDAP获取用户的所有组
c#-使用LDAP获取用户的所有组
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-使用LDAP获取用户的所有组,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3001字,纯文字阅读大概需要5分钟。
内容图文
我尝试从LDAP加载用户的所有组.
目前,我正在本地广告上进行测试.使用以下代码,我可以加载给定用户的所有组:
public IEnumerable<String> GetUserGroups( String userName )
{
using ( var domainContext = new PrincipalContext( ContextType.Domain, Name ) )
{
var user = UserPrincipal.FindByIdentity( domainContext, userName );
return user.GetAuthorizationGroups().Select( x => x.Name} ).ToList();
}
}
但是我无法使用LDAP获得相同的结果.
使用LDAP的代码:
public IEnumerable<String> GetUserGroups1(String userName)
{
//returns the container name of the given user
var containerName = GetUserContainerName(userName);
var groups = new List<String>();
if (containerName == null)
return groups;
var entry = new DirectoryEntry(String.Format("LDAP://{0}", "DC=example,DC=com"));
var searcher = new DirectorySearcher(entry)
{
Filter = String.Format("(member:{0}:=CN={1},{2},{3})",
"1.2.840.113556.1.4.1941",
containerName, "CN=Users", "DC=example,DC=com"),
SearchScope = SearchScope.Subtree
};
var result = searcher.FindAll();
for (var i = 0; i < result.Count; i++)
{
var path = result[i].Path;
var startIndex = path.IndexOf("CN=", StringComparison.Ordinal) + 3;
groups.Add(path.Substring(startIndex, path.IndexOf(",", startIndex + 1,
StringComparison.Ordinal) - startIndex));
}
return groups;
}
如何获取使用LDAP的用户的所有组?
解决方法:
我的第一个建议是,您应该对方法进行拆分,以便更好地了解一下:
>获取您的用户.
您可以使用类似这样的方法:
/// <summary>
/// Return the user by the user name
/// </summary>
/// <param name="userName_">Username to base search on</param>
/// <returns>
/// User Manager or null if not found
/// </returns>
public static DirectoryEntry SearchForUser(string userName_)
{
DirectoryEntry de = null;
DirectorySearcher directorySearcher = null;
Domain domain = null;
try
{
if (String.IsNullOrEmpty(userName_))
return null;
string userName = userName_.StartsWith("CN=") ? userName_.Replace("CN=", String.Empty) : userName_;
de = new DirectoryEntry("LDAP://" + Domain.GetCurrentDomain().Name);
directorySearcher = new DirectorySearcher(de);
directorySearcher.Filter = string.Format("(&(objectClass=person)(objectCategory=user)(sAMAccountname={0}))", userName);
SearchResult searchResult = directorySearcher.FindOne();
return searchResult != null ? searchResult_.GetDirectoryEntry() : null;
}
finally
{
if (de != null)
de.Dispose();
if (directorySearcher != null)
directorySearcher.Dispose();
if (domain != null)
domain.Dispose();
}
}
这样,您可以验证LDAP路径,域名,域
>获取所有组.
使用第二种方法可以清楚,简单地获取组.
/// <summary>
///Returns a list with the groups where this user is a member of.
/// </summary>
/// <remarks>The members in the returned list are instances of Group.</remarks>
/// <returns>Groups where this user is member of.</returns>
public List<DirectoryEntry> GetGroups()
{
return (from object o in Entry.Properties["memberOf"]
select new DirectoryEntry(path)
into dirEntry
where dirEntry.SchemaClassName == "group"
select {DirectoryEntry = dirEntry}).ToList();
}
其中path是您的OU路径(是否为root).
最大的挑战是管理和构建LDAP路径.
希望对您有所帮助.
内容总结
以上是互联网集市为您收集整理的c#-使用LDAP获取用户的所有组全部内容,希望文章能够帮你解决c#-使用LDAP获取用户的所有组所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。