- 浏览: 426284 次
- 性别:
- 来自: 唐山
文章分类
最新评论
-
hautbbs:
谢谢分享!
ASP.NET 导出Excel 和csv -
hautbbs:
感谢分享!
ASP.NET 导出Excel乱码的终极解决 -
wyf:
zcl920 写道只能说 看不懂。要发就发全 取一段出来 有什 ...
图片上绘制文字换行处理 -
zcl920:
只能说 看不懂。要发就发全 取一段出来 有什么用。
图片上绘制文字换行处理 -
380086154:
有用,谢谢。
js比较日期
public static class DomainInformation { #region Constants //static string[] usersLdapPath = @"http://www.cnblogs.com/yxbsmx/admin/ldap://zzzzzz.com/OU=xxxxxx,DC=yyyyyy,DC=com"; static string usersLdapPath = System.Configuration.ConfigurationManager.AppSettings["LDAPConnectionString"].ToString() ; const string adLoginName = "zzzzzz.com\\administrator"; //管理员用户 //或者上面写成 const string adLoginName =@ "zzzzzz.com\administrator"; //或者const string adLoginName = "administrator@zzzzzz.com"; 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; } } static public DataTable adUserlist( string groupname) //生成用户数据表 { DataTable dt = new DataTable(); dt.Columns.Add("cn", typeof(System.String)); dt.Columns.Add("sAMAccountName", typeof(System.String)); string[] groupmember = GetUsersForGroup(usersLdapPath[0], groupname); if (groupmember.Length == 0) { return null; } else { foreach (string member in groupmember) { if(IsAccountActive(getAccountControl(getAccountName(member)))) { DataRow dr = dt.NewRow(); dr[0] = member.ToString(); dr[1] = getAccountName(member); dt.Rows.Add(dr); } } return dt; } } static public void adUserlistbox(ListBox results, string groupName) //生成USER { results.Items.Clear(); DataTable dt = adUserlist(groupName); if (dt != null) { results.DataSource = dt; results.DataTextField = dt.Columns[0].Caption; results.DataValueField = dt.Columns[1].Caption; results.DataBind(); } } static public void adGrouplistbox(ListBox results) { results.Items.Clear(); DataTable dt = GetAdGroupInfo(); DataRow dr = dt.NewRow(); dr[1] = "All"; dr[2] = "All"; dt.Rows.Add(dr); results.DataSource = dt; results.DataTextField = dt.Columns[2].Caption; results.DataValueField = dt.Columns[1].Caption; results.DataBind(); } static public void aduserGrouplist(DropDownList results) { results.Items.Clear(); DataTable dt = GetAdGroupInfo(); results.DataSource = dt; results.DataTextField = dt.Columns[2].Caption; results.DataValueField = dt.Columns[1].Caption; results.DataBind(); } static public DirectoryEntry Directory() { DirectoryEntry userContainerDE; string accountName = UserProperty.getAccountName().ToString(); accountName = accountName.Substring(0, accountName.IndexOf("\\")).Trim(); //判断登陆用户是否为域用户,"zzzzzz"为域名,域名用户格式:zzzzzz/username if (accountName.ToLower() != "zzzzzz") userContainerDE = new DirectoryEntry(usersLdapPath, adLoginName, adLoginPassword); // AuthenticationTypes.Secure);} else userContainerDE = new DirectoryEntry(usersLdapPath);//, adLoginName, adLoginPassword); return userContainerDE; } static public DirectoryEntry Directoryunits(string ldappath ) { DirectoryEntry userContainerDE; string accountName = UserProperty.getAccountName().ToString(); accountName = accountName.Substring(0, accountName.IndexOf("\\")).Trim(); // userContainerDE = new DirectoryEntry(ldappath); if (accountName.ToLower() != "zzzzzz" ) //userContainerDE.Username = adLoginName; //userContainerDE.Password = adLoginPassword; //userContainerDE.AuthenticationType = AuthenticationTypes.Secure; userContainerDE = new DirectoryEntry(ldappath, adLoginName, adLoginPassword);// AuthenticationTypes.Secure);} else userContainerDE = new DirectoryEntry(ldappath);//, adLoginName, adLoginPassword); return userContainerDE; } public static int getAccountControl(string accountName)//获取权限码 { int results; DirectoryEntry userContainerDE = Directory(); DirectorySearcher ds = new DirectorySearcher(userContainerDE); ds.Filter = "(&(objectClass=user)(sAMAccountName=" + accountName + "))"; ds.PropertiesToLoad.Add("userAccountControl"); try { SearchResult r = ds.FindOne(); results = Convert.ToInt32(r.GetDirectoryEntry().InvokeGet("userAccountControl")); userContainerDE.Close(); return results; } catch { userContainerDE.Close(); return 0; } } public static bool IsAccountActive(int userAccountControl)//判断是否有效 { int ADS_UF_ACCOUNTDISABLE = 0X0002; int userAccountControl_Disabled = Convert.ToInt32(ADS_UF_ACCOUNTDISABLE); int flagExists = userAccountControl & userAccountControl_Disabled; if (flagExists > 0) return false; else return true; } public static DirectoryEntry GetDirectoryEntryByAccount(string sAMAccountName) { DirectoryEntry de = Directory(); DirectorySearcher deSearch = new DirectorySearcher(de); deSearch.Filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" + sAMAccountName + "))"; // deSearch.SearchScope = SearchScope.Subtree; try { SearchResult result = deSearch.FindOne(); //if (result == null) //{ return null; } de = Directoryunits(result.Path); return de; } catch { //throw; return null; } } public static DirectoryEntry GetDirectoryEntryByAccount(string sAMAccountName, string password) { DirectoryEntry de = GetDirectoryEntryByAccount(sAMAccountName); if (de != null) { // string commonName = de.Properties["cn"][0].ToString(); if (GetDirectoryEntry(sAMAccountName, password) != null) return GetDirectoryEntry(sAMAccountName, password); else return null; } else { return null; } } public static DirectoryEntry GetDirectoryEntry(string sAMAccountName, string password) { try { DirectoryEntry userde = new DirectoryEntry(usersLdapPath, sAMAccountName, password, AuthenticationTypes.Secure); DirectorySearcher deSearch = new DirectorySearcher(userde); deSearch.Filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" + sAMAccountName + "))"; //deSearch.SearchScope = SearchScope.Subtree; try { SearchResult result = deSearch.FindOne(); userde = Directoryunits(result.Path); return userde; } catch { //throw; return null; } } catch (Exception ex) { throw new Exception(ex.Message); } } }
发表评论
-
登录时记住用户名密码的实现方式
2019-06-12 15:11 2970登录的时候记住用户 ... -
CAS 实现单点登录 .NET MVC
2016-05-24 17:14 1079http://www.cnblogs.com/woxpp/p ... -
.NET开发邮件发送功能的全面教程(含邮件组件源码)
2015-03-31 09:43 1094原文地址:http://www.cnblogs.com/he ... -
开发Web组合
2015-01-04 11:39 5871、数据库操作 ORM-Dapper 2、前台界面布局采 ... -
基于 Bootstrap 构建的网站
2014-12-14 14:12 607文档,下载地址:http://v3.bootcss.com ... -
iis8 默认不支持svc解决方法
2014-09-18 18:57 731以下内容对于使用WIN2012 部署V9的时候使用。 ... -
C# 连接Oracle(利用ODP.net,不安装oracle客户端)
2014-07-11 09:37 1665C# 连接Oracle(利用ODP.net,不安装oracl ... -
C# Attribute 特性,过期特性
2014-05-27 15:18 1786通过下列过程将属性应用到代码元素。 通过从 .NE ... -
.NET画实时直方图
2011-12-30 09:37 879using System; using System.Col ... -
设置combobx选中项
2011-12-21 15:20 1005cbRole.SelectedIndex = cbRole.I ... -
文档树状结构化目录管理方法
2011-12-20 09:50 2100本文适用于附件(各类文档、图片和压缩包等,下同)比较多的 ... -
.StringTemplate替换模板
2011-11-03 10:19 1215官方下载 www.StringTemplate. ... -
WCF-IErrorHandler
2011-10-11 16:30 1010使用 IErrorHandler 接口,我们可以更深入地 ... -
ADODB.Stream instead of Scripting.FileSystemObject.
2011-07-04 08:55 1220In a Silverlight 4 OOB App (eve ... -
Scripting.FileSystemObject对象的详细技巧指南
2011-07-03 23:39 1018Scripting.FileSystemObject对象的 ... -
Stream 和 byte[] 之间的转换
2011-07-02 16:52 1046/* - - - - - - - - - - - - - ... -
常用正则表达式
2011-06-15 20:17 766正则表达式用于字符 ... -
DynamicMethod 类
2011-05-11 22:51 1121public delegate String MyMetho ... -
一个通用的快速反射方法(A General Fast Method Invoker)
2011-04-13 22:01 1495普通反射方法 MethodInfo methodIn ... -
C#操作IIS(转)可以写一个工具自己配置网站
2011-03-24 21:08 2241using System; using System.Dir ...
相关推荐
详细介绍了 C# 如何操作AD(活动目录) 附有代码Demo
活动目录辅助类。封装一系列活动目录操作相关的方法。包括用户登录验证及用户帐户属性修改等相关操作的方法
新版AD操作处理类 【ASP.NET组资源】新版AD操作处理类 关于ASP.NET如何获取AD中的详细用户信息等方法的实现。其中主要依托CLR:DirectoryServices. ** File Name: AdHandler.cs ** Creator: ** Create date: ** ...
一个asp.net工程,demo页面上进行域用户的认证,创建,删除操作
C#操作AD的几个例子,研究研究会有收获。
微软的AD的数据,通过C#进行同步的代码,C#同步AD的数据的代码,其中 体现了.Net操作AD的大部分API,对开发会有一定的帮助
AD域 OU及用户增加、删除、修改通用类库文件,其他分要的很贵,我下载了分享给大家,密码123456 希望对各位有用
1. 读取AD域用户、组。 2.创建或删除用户。 3.创建或删除组。 4.用户移动到组。 5.登录。 等..
刚开始开发域控时,费了好多时间,找到这个文档后,解决了不少问题。
如何在c#程序中模拟域帐户进行登录操作 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Principal; using System.Runtime.InteropServices; ...
C#AatviveDirectory操作源码
73. 系统操作相关的公共类 74. 缓存 75. 网站安全 76. 网站路径操作类 77. 网络 78. 视频帮助类 79. 视频转换类 80. 计划任务 81. 邮件 82. 邮件2 83. 配置文件操作类 84. 阿里云 85. 随机数类 86. ...
验证AD域账号登陆,获取AD域用户列表,获取用户邮箱,修改密码等AD域操作
2、课程的任务和要求 通过学习本课程使学生了解 C#和.net 开发环境的基本理论和操作,培养学 生使用.NET 平台开发应用程序的能力,主要教学内容以应用为主,结合开发环 境讲授 C#语言基本语法、面向对象编程方法、...
c#访问研华PCI1716 代码程序,通过线程读取PCI板卡 A/D数据转换结果
sharepoint引用,难免少不了要和AD打交道,使用程序对于AD的操作,相信大家很少用到,在这里共享一份AD操作功能类,希望对大家有所帮助。
C#读取AD域里用户名或组、Chart图形、cmd、Cookie&Session、CSVHelper、CSV文件转换、DataTable转实体、DBHelper、DecimalUtility及中文大写数字、Excel操作类、FTP操作类、H5-微信、Html操作类、INI文件读写类、IP...
本代码是对windows 域账户口进行操作,可以读取域名,LDAP地址,登录账号,用户登录验证,等等方法。 5分的资源分绝对值得。
根据用户帐号和密码取得用户的 对象,如果找到该用户,则返回用户的 对象;否则返回 null