一般公司局域网都会组建域,有的会使用域验证方式进行系统验证,所以了解C#对域的基本操作是必要的。
引用命名空间
C# Code | |
1. | using System.Security.Cryptography; |
2. | using System.Runtime.InteropServices; |
3. | using System.DirectoryServices; |
调用操作系统advapi32.dll动态链接库中的Api函数
C# Code | |
1. | [DllImport('advapi32.dll')] |
2. | private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); |
3. | const int LOGON32_LOGON_INTERACTIVE = 2; //通过网络验证账户合法性 |
4. | const int LOGON32_PROVIDER_DEFAULT = 0; //使用默认的Windows 2000/NT NTLM验证方 |
域验证函数
C# Code | |
1. | private bool chkPassword(string account, string password) |
2. | { |
3. | IntPtr tokenHandle = new IntPtr(0); |
4. | tokenHandle = IntPtr.Zero; |
5. | string domainName = 'youradname'; |
6. | if (LogonUser(account, domainName, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle)) |
7. | return true; |
8. | return false; |
9. | } |
以域管理员身份登录并查找域帐号
C# Code | |
1. | DirectorySearcher ds = new DirectorySearcher(); |
2. | ds.SearchRoot = new DirectoryEntry('LDAP://192.168.1.1/DC=域名称,DC=域名,DC=com,DC=cn', 'adminname', 'adminpassword'); |
3. | ds.Filter = '(objectClass=user)'; |
4. | ds.SearchScope = SearchScope.Subtree; |
5. | ds.Sort = new SortOption('Name', System.DirectoryServices.SortDirection.Ascending); |
6. | ds.PageSize = 1024; |
7. | SearchResultCollection rs = ds.FindAll(); |
8. | foreach (SearchResult r in rs) |
9. | { |
10. | ResultPropertyCollection rprops = r.Properties; |
11. | string result = null; |
12. | foreach (string name in rprops.PropertyNames) |
13. | { |
14. | if (name.Equals('samaccountname')) |
15. | { |
16. | foreach (object vl in rprops[name]) |
17. | result = Console.WriteLine(vl.ToString()); |
18. | } |
19. | } |
20. | } |
修改密码
C# Code | |
1. | string path = 'LDAP://192.168.1.1/DC=youradname,DC=huiyaosoft,DC=com,DC=cn'; |
2. | DirectoryEntry de = new DirectoryEntry(path, @'youradname\admin', 'password', AuthenticationTypes.Secure); |
3. | DirectorySearcher ds = new DirectorySearcher(de); |
4. | //ds.SearchScope = SearchScope.Subtree; |
5. | //ds.Filter = '(&(objectClass=user)(cn=张三))'; |
6. | ds.Filter = '(&(objectClass=user)(sAMAccountName=zhangsan))'; |
7. | //ds.SearchScope = SearchScope.Subtree; |
8. | //ds.Sort = new SortOption('Name', System.DirectoryServices.SortDirection.Ascending); |
9. | //ds.PageSize = 1024; |
10. | SearchResult result = ds.FindOne(); |
11. | if (result != null) |
12. | { |
13. | DirectoryEntry userEntry = result.GetDirectoryEntry(); |
14. | if (userEntry != null) |
15. | { |
16. | try |
17. | { |
18. | Console.WriteLine(userEntry.Path); |
19. | userEntry.Invoke('SetPassword', new object[] { 'huiyaosoft.com' }); |
20. | userEntry.CommitChanges(); |
21. | userEntry.Close(); |
22. | Console.WriteLine('Ok'); |
23. | } |
24. | catch (Exception ex) |
25. | { |
26. | Console.WriteLine( ex.ToString()); |
27. | } |
28. | } |
29. |
} 来源:http://www.huiyaosoft.com/html/csharpad.htm C#域验证及遍历域帐号 |