代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
ASP.NET访问域用户(AD活动目录)信息的类
作者:
Dezai.CN
/ 发布于
2012/12/20
/
704
public static class DomainInformation { #region Constants //static string[] usersLdapPath = @"LDAP://zzzzzz.com/OU=xxxxxx,DC=yyyyyy,DC=com"; static string usersLdapPath = System.Configuration.ConfigurationManager.AppSettings["LDAPConnectionString"].ToString() ; const string adLoginName = "administrator"; //管理员用户 const string adLoginPassword = "88888888"; #endregion static public string[] GetGroupsForUser(string domainADsPath, string username)// 获取用户所属组 { DirectoryEntry usersDE = Directoryunits(domainADsPath); DirectorySearcher ds = new DirectorySearcher(usersDE); ds.Filter = "(&(sAMAccountName=" + username + "))"; ds.PropertiesToLoad.Add("memberof"); SearchResult r = ds.FindOne(); if (r.Properties["memberof"].Count == 0) { return (null); } string[] results = new string[r.Properties["memberof"].Count]; for (int i = 0; i < r.Properties["memberof"].Count; i++) { string theGroupPath = r.Properties["memberof"][i].ToString(); results[i] = theGroupPath.Substring(3, theGroupPath.IndexOf(",") - 3); } usersDE.Close(); return (results); } /// <summary> /// </summary> /// <param name="username"></param> /// <returns></returns> public static string[] GetGroupsForUser(string username) { DirectoryEntry usersDE = DomainInformation.Directory(); DirectorySearcher ds = new DirectorySearcher(usersDE); ds.Filter = "(&(sAMAccountName=" + username + "))"; ds.PropertiesToLoad.Add("memberof"); SearchResult r = ds.FindOne(); if (r.Properties["memberof"] == null) { return (null); } string[] results = new string[r.Properties["memberof"].Count+1]; for (int i = 0; i < r.Properties["memberof"].Count; i++) { string theGroupPath = r.Properties["memberof"][i].ToString(); results[i] = theGroupPath.Substring(3, theGroupPath.IndexOf(",") - 3); } results[r.Properties["memberof"].Count]="All";//All组属于任何人,在AD之外定义了一个组,以便分配用户权限 usersDE.Close(); return (results); } static public string[] GetUsersForGroup(string domainADsPath, string Groupname)// 获取用户 { DirectoryEntry usersDE = Directoryunits(domainADsPath); DirectorySearcher ds = new DirectorySearcher(usersDE); ds.Filter = "(&(objectClass=group)(cn=" + Groupname + "))"; ds.PropertiesToLoad.Add("member"); SearchResult r = ds.FindOne(); if (r.Properties["member"] == null) { return (null); } string[] results = new string[r.Properties["member"].Count]; for (int i = 0; i < r.Properties["member"].Count; i++) { string theGroupPath = r.Properties["member"][i].ToString(); results[i] = theGroupPath.Substring(3, theGroupPath.IndexOf(",") - 3); } usersDE.Close(); return (results); } static public string GetUserDisplayName(string username)// 获取组用户 { string results; DirectoryEntry usersDE = Directory(); DirectorySearcher ds = new DirectorySearcher(usersDE); ds.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))"; ds.PropertiesToLoad.Add(UserProperty.DisplayName); SearchResult r = ds.FindOne(); results = r.GetDirectoryEntry().InvokeGet(UserProperty.DisplayName).ToString(); usersDE.Close(); return (results); } static public UserInfoEx GetUserInfoEx(string username) //获取域用户详细信息 { DirectoryEntry usersDE =Directory(); DirectorySearcher ds = new DirectorySearcher(usersDE); ds.Filter = "(&(objectClass=user)(objectCatogery=person)(sAMAccountName=" + username + "))"; ds.PropertiesToLoad.Add("cn"); ds.PropertiesToLoad.Add(UserProperty.Name); ds.PropertiesToLoad.Add(UserProperty.UserName); ds.PropertiesToLoad.Add(UserProperty.homePhone); ds.PropertiesToLoad.Add(UserProperty.FirstName); ds.PropertiesToLoad.Add(UserProperty.LastName); ds.PropertiesToLoad.Add(UserProperty.Email); ds.PropertiesToLoad.Add(UserProperty.Title); ds.PropertiesToLoad.Add(UserProperty.Company); ds.PropertiesToLoad.Add(UserProperty.Address); ds.PropertiesToLoad.Add(UserProperty.City); ds.PropertiesToLoad.Add(UserProperty.State); ds.PropertiesToLoad.Add(UserProperty.PostalCode); ds.PropertiesToLoad.Add(UserProperty.Phone); ds.PropertiesToLoad.Add(UserProperty.Country); SearchResult r = ds.FindOne(); UserInfoEx result = new UserInfoEx(); result.Name = r.GetDirectoryEntry().InvokeGet(UserProperty.Name).ToString(); result.LoginName = r.GetDirectoryEntry().InvokeGet(UserProperty.UserName).ToString(); if (r.GetDirectoryEntry().InvokeGet(UserProperty.FirstName) != null) { result.FirstName = r.GetDirectoryEntry().InvokeGet(UserProperty.FirstName).ToString(); } else { result.FirstName = ""; } if (r.GetDirectoryEntry().InvokeGet(UserProperty.homePhone) != null) { result.homePhone = r.GetDirectoryEntry().InvokeGet(UserProperty.homePhone).ToString(); } else { result.homePhone = ""; } if (r.GetDirectoryEntry().InvokeGet(UserProperty.LastName)!= null) { result.LastName = r.GetDirectoryEntry().InvokeGet(UserProperty.LastName).ToString(); } else { result.LastName = ""; } if (r.GetDirectoryEntry().InvokeGet(UserProperty.Email) != null) { result.EmailAddress = r.GetDirectoryEntry().InvokeGet(UserProperty.Email).ToString(); } else { result.EmailAddress = ""; } if (r.GetDirectoryEntry().InvokeGet(UserProperty.Title) != null) { result.Title = r.GetDirectoryEntry().InvokeGet(UserProperty.Title).ToString(); } else { result.Title = ""; } if (r.GetDirectoryEntry().InvokeGet(UserProperty.Company) != null) { result.Company =r.GetDirectoryEntry().InvokeGet(UserProperty.Company).ToString(); } else { result.Company = ""; } if (r.GetDirectoryEntry().InvokeGet(UserProperty.Address) != null) { result.Address =r.GetDirectoryEntry().InvokeGet(UserProperty.Address).ToString(); } else { result.Address = ""; } if (r.GetDirectoryEntry().InvokeGet(UserProperty.City) != null) { result.City =r.GetDirectoryEntry().InvokeGet(UserProperty.City).ToString(); } else { result.City = ""; } if (r.GetDirectoryEntry().InvokeGet(UserProperty.State) != null) { result.State =r.GetDirectoryEntry().InvokeGet(UserProperty.State).ToString(); } else { result.State = ""; } if (r.GetDirectoryEntry().InvokeGet(UserProperty.PostalCode) != null) { result.PostalCode =r.GetDirectoryEntry().InvokeGet(UserProperty.PostalCode).ToString(); } else { result.PostalCode = ""; } if (r.GetDirectoryEntry().InvokeGet(UserProperty.Phone) != null) { result.Phone = r.GetDirectoryEntry().InvokeGet(UserProperty.Phone).ToString(); } else { result.Phone = ""; } if (r.GetDirectoryEntry().InvokeGet(UserProperty.Country) != null) { result.Country =r.GetDirectoryEntry().InvokeGet(UserProperty.Country).ToString(); } else { result.Country = ""; } usersDE.Close(); return (result); } static private string GetAdGroupDescription(string prefix)//根据CN获取组description { string results; DirectoryEntry groupsDE = Directory(); DirectorySearcher groupsDS = new DirectorySearcher(groupsDE); groupsDS.Filter = "(&(objectClass=group)(CN=" + prefix + "*))"; groupsDS.PropertiesToLoad.Add("cn"); SearchResult sr = groupsDS.FindOne(); results = sr.GetDirectoryEntry().InvokeGet("description").ToString(); groupsDE.Close(); return (results); } static private DataTable GetAdGroupInfo()//根据CN获取组信息 { DataTable dt = new DataTable(); dt.Columns.Add("URL", typeof(System.String)); dt.Columns.Add("cn", typeof(System.String)); dt.Columns.Add("Description", typeof(System.String)); DirectoryEntry groupsDE = Directory(); DirectorySearcher searcher = new DirectorySearcher(groupsDE); searcher.Filter = "(&(objectClass=group))"; //searcher.SearchScope = SearchScope.Subtree; //searcher.Sort = new SortOption("description", System.DirectoryServices.SortDirection.Ascending); searcher.PropertiesToLoad.AddRange(new string[] { "cn", "description"}); SearchResultCollection results = searcher.FindAll(); if (results.Count == 0) { return (null); } else { foreach (SearchResult result in results) { DataRow dr = dt.NewRow(); dr[0] = result.Path.ToString(); dr[1] = result.GetDirectoryEntry().InvokeGet("cn").ToString(); if (result.GetDirectoryEntry().InvokeGet("Description")!=null) dr[2] = result.GetDirectoryEntry().InvokeGet("Description").ToString(); else dr[2] = result.GetDirectoryEntry().InvokeGet("cn").ToString(); dt.Rows.Add(dr); } dt.DefaultView.Sort = "description ASC"; groupsDE.Close(); return dt; } } static public string getAccountName(string cn) //根据CN获取登陆名 { foreach (string path in usersLdapPath) { DirectoryEntry userContainerDE = Directoryunits(path); DirectorySearcher ds = new DirectorySearcher(userContainerDE); ds.Filter = "(&(objectClass=user)(cn=*" + cn + "*))"; ds.PropertiesToLoad.Add("sAMAccountName"); SearchResult r = ds.FindOne(); if (r!=null) return r.GetDirectoryEntry().InvokeGet("sAMAccountName").ToString(); } return null; } static public bool isAdUser(string username)//判断是否域用户 { DirectoryEntry userContainerDE = Directory(); DirectorySearcher ds = new DirectorySearcher(userContainerDE); ds.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))"; ds.PropertiesToLoad.Add("cn"); SearchResult r = ds.FindOne(); if (r == null) { userContainerDE.Close(); return false; } else { userContainerDE.Close(); return true; } }
试试其它关键字
访问域用户
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3