操作域用户!

MCS给了二个操作域用户的类!记录如下
比如好用,省的以后用了再去找
ActiveDirectoryHelper

  1 None.gif public   class  ActiveDirectoryHelper
  2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  3InBlock.gif    public static string RootPath = "";
  4InBlock.gif    public static string AdminUsername = "";
  5InBlock.gif    public static string AdminPassword = "";
  6InBlock.gif    public static DirectoryEntry GetDirectoryEntry(string path, string username, string password)
  7ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  8InBlock.gif        DirectoryEntry de = new DirectoryEntry();
  9InBlock.gif        de.Path = path;
 10InBlock.gif        de.Username = username;
 11InBlock.gif        de.Password = password;
 12InBlock.gif
 13InBlock.gif        RootPath = path;
 14InBlock.gif        AdminUsername = username;
 15InBlock.gif        AdminPassword = password;
 16InBlock.gif
 17InBlock.gif        return de;
 18ExpandedSubBlockEnd.gif    }

 19InBlock.gif
 20InBlock.gif    public static string CreateNewUser(DirectoryEntry entry, ActiveDirectoryUser adUser, string groupName)
 21ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 22InBlock.gif        DirectoryEntries users = entry.Children;
 23InBlock.gif        DirectoryEntry newUser = users.Add("CN=" + adUser.LoginName, "user");
 24InBlock.gif
 25InBlock.gif        SetProperty(newUser, "employeeID", adUser.EmployeeID);
 26InBlock.gif        SetProperty(newUser, "SAMAccountName", adUser.LoginName);
 27InBlock.gif        SetProperty(newUser, "userPrincipalName", adUser.LoginName);
 28InBlock.gif
 29InBlock.gif        string password = SetPassword(newUser.Path);
 30InBlock.gif        newUser.CommitChanges();
 31InBlock.gif
 32InBlock.gif        EnableAccount(newUser);
 33InBlock.gif
 34InBlock.gif        AddUserToGroup(entry, newUser, groupName);
 35InBlock.gif
 36InBlock.gif        newUser.Close();
 37InBlock.gif        entry.Close();
 38InBlock.gif        return password;
 39ExpandedSubBlockEnd.gif    }

 40InBlock.gif
 41InBlock.gif    public static void SetProperty(DirectoryEntry entry, string propertyName, string propertyValue)
 42ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 43InBlock.gif        if (!string.IsNullOrEmpty(propertyValue))
 44ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 45InBlock.gif            if (entry.Properties.Contains(propertyName))
 46ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 47InBlock.gif                entry.Properties[propertyName][0= propertyValue;
 48ExpandedSubBlockEnd.gif            }

 49InBlock.gif            else
 50ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 51InBlock.gif                entry.Properties[propertyName].Add(propertyValue);
 52ExpandedSubBlockEnd.gif            }

 53ExpandedSubBlockEnd.gif        }

 54ExpandedSubBlockEnd.gif    }

 55InBlock.gif
 56InBlock.gif    public static string GetProperty(DirectoryEntry entry, string propertyName)
 57ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 58InBlock.gif        if (entry.Properties.Contains(propertyName))
 59InBlock.gif            return entry.Properties[propertyName][0].ToString();
 60InBlock.gif        else
 61InBlock.gif            return String.Empty;
 62ExpandedSubBlockEnd.gif    }

 63InBlock.gif
 64InBlock.gif    public static string SetPassword(string path)
 65ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 66InBlock.gif        DirectoryEntry user = new DirectoryEntry();
 67InBlock.gif        user.Path = path;
 68InBlock.gif        user.AuthenticationType = AuthenticationTypes.Secure;
 69InBlock.gif        string password = "RandomPassword.Generate()";
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        object[] pw = new object[] dot.gif{ password };
 71InBlock.gif        object ret = user.Invoke("SetPassword", pw);
 72InBlock.gif        user.CommitChanges();
 73InBlock.gif        user.Close();
 74InBlock.gif        return password;
 75ExpandedSubBlockEnd.gif    }

 76InBlock.gif
 77InBlock.gif    public static void EnableAccount(DirectoryEntry entry)
 78ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 79InBlock.gif        // UF_DONT_EXPIRE_PASSWD 0x0001
 80InBlock.gif        int exp = (int)entry.Properties["userAccountControl"].Value;
 81InBlock.gif        entry.Properties["userAccountControl"].Value = exp | 0x0001;
 82InBlock.gif        entry.CommitChanges();
 83InBlock.gif        // UF_ACCOUNTDISABLE 0x0002
 84InBlock.gif        int val = (int)entry.Properties["userAccountControl"].Value;
 85InBlock.gif        entry.Properties["userAccountControl"].Value = val & ~0x0002;
 86InBlock.gif        entry.CommitChanges();
 87ExpandedSubBlockEnd.gif    }

 88InBlock.gif
 89InBlock.gif    public static void DisableAccount(DirectoryEntry rootEntry, string employeeID)
 90ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 91InBlock.gif        DirectorySearcher searcher = new DirectorySearcher(rootEntry);
 92InBlock.gif        searcher.Filter = "(&(objectCategory=Person)(objectClass=user)(employeeID=" + employeeID + "))";
 93InBlock.gif        searcher.SearchScope = SearchScope.Subtree;
 94InBlock.gif        SearchResult result = searcher.FindOne();
 95InBlock.gif
 96InBlock.gif        if (result != null)
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 98InBlock.gif            DirectoryEntry entry = GetDirectoryEntry(result.Path, AdminUsername, AdminPassword);
 99InBlock.gif            int val = (int)entry.Properties["userAccountControl"].Value;
100InBlock.gif            entry.Properties["userAccountControl"].Value = val | 0x0002;
101InBlock.gif            entry.Properties["msExchHideFromAddressLists"].Value = "TRUE";
102InBlock.gif            entry.CommitChanges();
103InBlock.gif            entry.Close();
104ExpandedSubBlockEnd.gif        }

105InBlock.gif
106InBlock.gif        rootEntry.Close();
107ExpandedSubBlockEnd.gif    }

108InBlock.gif
109InBlock.gif    public static void AddUserToGroup(DirectoryEntry entry, DirectoryEntry entryUser, string groupName)
110ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
111InBlock.gif        DirectorySearcher searcher = new DirectorySearcher();
112InBlock.gif        searcher.SearchRoot = entry;
113InBlock.gif        searcher.Filter = "(&(objectClass=group) (cn=" + groupName + "))";
114InBlock.gif        SearchResultCollection results = searcher.FindAll();
115InBlock.gif
116InBlock.gif        bool isGroupMember = false;
117InBlock.gif        if (results.Count > 0)
118ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
119InBlock.gif            DirectoryEntry group = GetDirectoryEntry(results[0].Path, AdminUsername, AdminPassword);
120InBlock.gif            object members = group.Invoke("Members"null);
121InBlock.gif            foreach (object member in (IEnumerable)members)
122ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
123InBlock.gif                DirectoryEntry x = new DirectoryEntry(member);
124InBlock.gif                if (x.Name != entryUser.Name)
125InBlock.gif                    isGroupMember = false;
126InBlock.gif                else
127ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
128InBlock.gif                    isGroupMember = true;
129InBlock.gif                    break;
130ExpandedSubBlockEnd.gif                }

131ExpandedSubBlockEnd.gif            }

132InBlock.gif            if (!isGroupMember)
133ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
134ExpandedSubBlockStart.gifContractedSubBlock.gif                group.Invoke("Add"new object[] dot.gif{ entryUser.Path.ToString() });
135ExpandedSubBlockEnd.gif            }

136InBlock.gif            group.Close();
137ExpandedSubBlockEnd.gif        }

138InBlock.gif        return;
139ExpandedSubBlockEnd.gif    }

140InBlock.gif
141InBlock.gif    public static DirectoryEntry UserExists(DirectoryEntry entry, string username)
142ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
143InBlock.gif        DirectorySearcher searcher = new DirectorySearcher(entry);
144InBlock.gif       // searcher.Filter = "(&(objectClass=user)(cn=" + username + "))";
145InBlock.gif        searcher.Filter = "(&(objectClass=user)(samAccountName=" + username + "))";
146InBlock.gif        SearchResultCollection results = searcher.FindAll();
147InBlock.gif        entry.Close();
148InBlock.gif        if (results.Count == 0)
149InBlock.gif            return null;
150InBlock.gif        else
151InBlock.gif            return results[0].GetDirectoryEntry();
152ExpandedSubBlockEnd.gif    }

153InBlock.gif
154InBlock.gif    public static ActiveDirectoryUser GetUserInformation(DirectoryEntry userEntry)
155ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
156InBlock.gif        ActiveDirectoryUser adUser = new ActiveDirectoryUser();
157InBlock.gif        adUser.EmployeeID = GetProperty(userEntry, "employeeID");
158InBlock.gif        adUser.Email = GetProperty(userEntry, "mail");
159InBlock.gif
160InBlock.gif        userEntry.Close();
161InBlock.gif        return adUser;
162ExpandedSubBlockEnd.gif    }

163InBlock.gif
164InBlock.gif    public static void UpdateUserInformation(DirectoryEntry rootEntry, ActiveDirectoryUser adUser)
165ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
166InBlock.gif        DirectorySearcher searcher = new DirectorySearcher(rootEntry);
167InBlock.gif        searcher.Filter = "(&(objectCategory=Person)(objectClass=user)(employeeID=" + adUser.EmployeeID + "))";
168InBlock.gif        searcher.SearchScope = SearchScope.Subtree;
169InBlock.gif        SearchResult result = searcher.FindOne();
170InBlock.gif
171InBlock.gif        if (result != null)
172ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
173InBlock.gif            DirectoryEntry userEntry = result.GetDirectoryEntry();
174InBlock.gif            //               SetProperty(userEntry, "
175ExpandedSubBlockEnd.gif        }

176InBlock.gif        rootEntry.Close();
177ExpandedSubBlockEnd.gif    }

178ExpandedBlockEnd.gif}

179 None.gif
实体类ActiveDirectoryUser
 1 None.gif public   class  ActiveDirectoryUser
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3InBlock.gif    private string employeeID = "FPC00xxx";
 4InBlock.gif    public string EmployeeID
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 6ExpandedSubBlockStart.gifContractedSubBlock.gif        get dot.gifreturn employeeID; }
 7InBlock.gif        set
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 9InBlock.gif            //Regex rx = new Regex(@"^FPC\d{5}");
10InBlock.gif            //if (rx.IsMatch(value))
11InBlock.gif                employeeID = value;
12InBlock.gif            //else
13InBlock.gif            //    throw new ArgumentException("EmployeeID应该为FPCxxxxx,x代表数字", "EmployeeID");
14ExpandedSubBlockEnd.gif        }

15ExpandedSubBlockEnd.gif    }

16InBlock.gif
17InBlock.gif    private string loginName = "User";
18InBlock.gif    public string LoginName
19ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
20ExpandedSubBlockStart.gifContractedSubBlock.gif        get dot.gifreturn loginName; }
21InBlock.gif        set
22ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
23InBlock.gif            Regex rx = new Regex("[0-9a-zA-Z]{3,10}");
24InBlock.gif            if (rx.IsMatch(value))
25InBlock.gif                loginName = value;
26InBlock.gif            else
27InBlock.gif                throw new ArgumentException("登录名应该是数字和字母的组合,并且在3-10个字符之间""LoginName");
28ExpandedSubBlockEnd.gif        }

29ExpandedSubBlockEnd.gif    }

30InBlock.gif
31InBlock.gif    private string password = "Pass@word1";
32InBlock.gif    public string Password
33ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
34ExpandedSubBlockStart.gifContractedSubBlock.gif        get dot.gifreturn password; }
35ExpandedSubBlockStart.gifContractedSubBlock.gif        set dot.gif{ password = value; }
36ExpandedSubBlockEnd.gif    }

37InBlock.gif
38InBlock.gif    private string email = "xxx@***.com.cn";
39InBlock.gif    public string Email
40ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
41ExpandedSubBlockStart.gifContractedSubBlock.gif        get dot.gifreturn email; }
42ExpandedSubBlockStart.gifContractedSubBlock.gif        set dot.gif{ email = value; }
43ExpandedSubBlockEnd.gif    }

44ExpandedBlockEnd.gif}

45 None.gif

转载于:https://www.cnblogs.com/listhome/archive/2006/11/10/556597.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值