如何取到当前AD(活动目录)用户属于哪个AD组?

haity666 2010-09-21 01:31:50
目前只能使用User.Identity.Name取到当前登录的AD用户名,如何取到此AD用户属于哪个AD组呢?
...全文
962 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
等待高手、
haity666 2010-09-23
  • 打赏
  • 举报
回复
顶顶顶顶顶
haity666 2010-09-21
  • 打赏
  • 举报
回复
求代码啊
大腹 2010-09-21
  • 打赏
  • 举报
回复
嗯,七楼正解。
以前做过这个,通常是从Properties里边去取。
请去Microsoft查找操作ad的bit程序代码,去仿写一下就好了。
haity666 2010-09-21
  • 打赏
  • 举报
回复
运行 5楼得到的是:

未知错误(0x80005000)
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Runtime.InteropServices.COMException: 未知错误(0x80005000)
wjn161 2010-09-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 haity666 的回复:]
楼上的也没有取到域里面的组啊
[/Quote]

Properties["memberOf"]
porschev 2010-09-21
  • 打赏
  • 举报
回复
看看。。又是这个问题
zzxap 2010-09-21
  • 打赏
  • 举报
回复
# 假设AD中有一组织单位,给定以下信息:  
#
# AD:ms.com
#
# AD管理员:administrator
#
# AD管理员密码:pass@word1
#
# 组织单位名称:XX有限公司(不必理会其下有嵌套多少组织单位,通常都是部门)
#
# 现在要获取这一组织单位下的所有的用户信息,比如只要:帐号,姓名,邮件,所在组织这四个字段,具体实现如示例代码所示:
#
# private const string domainName = "ms.com";
#
# private const string adAdmin = "administrator";
#
# private const string password = "pass@word1";
#
# private const string ouName = "XX有限公司";
#
# private DataTable GetADUsers()
#
# {
#
# DataTable dt = new DataTable();
#
# dt.Columns.Add("sAMAccountName");//帐号
#
# dt.Columns.Add("Name");//姓名
#
# dt.Columns.Add("mail"); //邮箱地址
#
# dt.Columns.Add("OU"); //用户组织
#
# DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + domainName, adAdmin, password, AuthenticationTypes.Secure);
#
# DirectoryEntry ou = adRoot.Children.Find("OU=" + ouName);
#
# DirectorySearcher mySearcher = new DirectorySearcher(ou);
#
# mySearcher.Filter = ("(objectClass=user)"); //user表示用户,group表示组
#
# foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
#
# {
#
# DataRow dr = dt.NewRow();
#
# dr["sAMAccountName"] = string.Empty;
#
# dr["Name"] = string.Empty;
#
# dr["mail"] = string.Empty;
#
# dr["OU"] = string.Empty;
#
# DirectoryEntry user = resEnt.GetDirectoryEntry();
#
# if (user.Properties.Contains("sAMAccountName"))
#
# {
#
# dr["sAMAccountName"] = user.Properties["sAMAccountName"][0].ToString();
#
# }
#
# if (user.Properties.Contains("Name"))
#
# {
#
# dr["Name"] = user.Properties["Name"][0].ToString();
#
# }
#
# if (user.Properties.Contains("mail"))
#
# {
#
# dr["mail"] = user.Properties["mail"][0].ToString();
#
# }
#
# if (user.Parent.Name != string.Empty && user.Parent.Name.IndexOf('=') > -1)
#
# {
#
# //获取用户所在的组织单位
#
# dr["OU"] = user.Parent.Name.Split('=')[1];
#
# }
#
# dt.Rows.Add(dr);
#
# }
#
# return dt;
#
# }
#
# }
#
# 如果想要知道用户信息中都包含哪些字段,可以foreach出来看看
#
# DirectoryEntry user = resEnt.GetDirectoryEntry();
#
# foreach (string property in user.Properties.PropertyNames)
#
# {
#
# Console.WriteLine("字段名: " + property);
#
# }
haity666 2010-09-21
  • 打赏
  • 举报
回复
楼上的也没有取到域里面的组啊
wjn161 2010-09-21
  • 打赏
  • 举报
回复

以前对域操作的一些代码。希望对你有帮助

private string GetNamingContext(string hostName, string userName, string Pwd)
{
using (DirectoryEntry de = new DirectoryEntry())
{
string path = String.Format("LDAP://{0}/rootDSE", hostName);
de.Username = userName;
de.Password = Pwd;
de.Path = path;
return de.Properties["defaultNamingContext"][0].ToString();
}
}

//hostName为域名
public List<User> GetAllUsers(string hostName,string userName,string Pwd)
{
List<User> userList = new List<User>();
using (DirectoryEntry root = new DirectoryEntry())
{
string defaultNamingContext = GetNamingContext(hostName, userName, Pwd);
root.Path = string.Format("LDAP://{0}/{1}", hostName, defaultNamingContext);
root.Username = userName;
root.Password = Pwd;
using (DirectorySearcher searcher = new DirectorySearcher())
{
searcher.SearchRoot = root;
searcher.SearchScope = SearchScope.Subtree;
searcher.Filter = "(&(objectCategory=person)(objectClass=user))";
SearchResultCollection results = searcher.FindAll();
foreach (SearchResult result in results)
{
User user = new User();
user.UserID = result.Properties["sAMAccountName"][0].ToString(); //用户登录名
//某个属性可以取到userGroup,查下MSDN
user.UserName = result.Properties["Name"][0].ToString();//用户姓名
userList.Add(user);
}
}
}
return userList;
}

yqyqyoyo 2010-09-21
  • 打赏
  • 举报
回复
恕我愚笨,没看懂LZ说的是啥意思。

62,272

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧