Tài liệu Làm Việc Với Active Directory part 5 docx

5 303 0
Tài liệu Làm Việc Với Active Directory part 5 docx

Đang tải... (xem toàn văn)

Thông tin tài liệu

Tìm kiếm các đối tượng người dùng Trong phần này ta sẽ xây dựng 1 Windows Form gọi là UserSearch .trong ứng dụng ta có thể nhập domain controller, uesrname ,password để truy nhập Active Directory. ta sẽ truy nhập vào schema của Active Directory để lấy các thuộc tính của đối tượng user. người dùng có thể nhập chuỗi tìm các đối tưọng user trong domain. ngoài ra ta cũng có thể thiết lập các thuộc tính của đối tượng user nên được trình bày. Giao diện người dùng Các bước sử dụng : 1. Nhập Username ,PasswordmDomain controller. tất cả đều tuỳ chọn . nếu không nhập domain controller thì kết nối làm việ c với liên kết không server ( serverless binding).nếu không nhập username thì ngữ cảnh của người dùng hiện tại được lấy. 2. 1 nút nhấn cho phép tất cả các tên thuộc tính của đối tượng user được tải vào trong listbox listboxProperties 3. Sau khi tên thuộc tính được tải ta có thể lựa chọn các thuộc tính . selectioinmode của listbox được đặt là MultiSimple. 4. Nhập vào filter để giới hạn tìm kiếm. giá trị mặc định là (objectClass=user) 5. Bắt đầu tìm kiếm Ngữ cảnh tên Schema ( Schema Naming Context) Chươ ng trình có 2 phương thức xử lý : đầu tiên là nút nhấn để tải các thuộc tính, thứ 2 là nút bắt đầu tìm kiếm trong domain.phần đầu ta đọc các thuộc tính trong schema để trình bày nó. Trong phương thức buttonLoActive DirectoryProperties_Click() ,SetLogonInformation() đọc username,password và hostname từ hộp thoại và lưu chúng trong các biến thành viên của lớp.sau đó phương thức SetNamingContext() đặt tên LDAP của Schema và tên LDAP của ngữ cảnh mặc định .tên Schema LDAP này được dùng để đặt các thuộc tính trong listbox : SetUserProperties(): private void buttonLoadProperties_Click(object sender, System.EventArgs e) { try { SetLogonInformation(); SetNamingContext(); SetUserProperties(schemaNamingContext); } catch (Exception ex) { MessageBox.Show("Check your inputs! " + ex.Message); } } protected void SetLogonInformation() { username = (textBoxUsername.Text == "" ? null : textBoxUsername.Text); password = (textBoxPassword.Text == "" ? null : textBoxPassword.Text); hostname = textBoxHostname.Text; if (hostname != "") hostname += "/"; } Hổ trợ cho phương thức SetNamingContext(), ta dùng gốc của cây thư mục để lấy các thuộc tính của server, ta chỉ quan tâm đến giá trị 2 thuộc tính : schemaNamingContext và defaultNamingContext : protected string SetNamingContext() { using (DirectoryEntry de = new DirectoryEntry()) { string path = "LDAP://" + hostname + "rootDSE"; de.Username = username; de.Password = password; de.Path = path; schemaNamingContext = de.Properties["schemaNamingContext"][0].ToString(); defaultNamingContext = de.Properties["defaultNamingContext"][0].ToString(); } } Lấy các tên thuộc tính của lớp User Ta có tên LDAP để truy nhập schema . ta có thể dùng tên này để truy nhập thư mục và đọc các thuộc tính. ta không chỉ quan tâm đến lớp user mà còn các lớp cơ sở khác như : Organizational-Person, Person, and Top. trong chương trình này tên của các lớp cơ sở là hard-coded ( mã chỉ định sẵn ) . nếu muốn đọc ta dùng thuộc tính subclassof .GetSchemaProperties() trả về 1 mảng chuỗi với tên tất cả thuộc tính của kiểu đối tượng đặc tả .tất c ả tên thuộc tính được thu thập vào thuộc tính StringCollection : protected void SetUserProperties(string schemaNamingContext) { StringCollection properties = new StringCollection(); string[] data = GetSchemaProperties(schemaNamingContext, "User"); properties.AddRange(GetSchemaProperties(schemaNamingContext, "Organizational-Person")); properties.AddRange(GetSchemaProperties(schemaNamingContext, "Person")); properties.AddRange(GetSchemaProperties(schemaNamingContext, "Top")); listBoxProperties.Items.Clear(); foreach (string s in properties) { listBoxProperties.Items.Add(s); } } Trong GetSchemaProperties() ta truy nhập Active Directory lại, lần này rootDSE không được dùng.thuộc tính SystemMayContain giữ 1 tập tất cả các thuộc tính mà được cho phép trong lớp objectType: protected string[] GetSchemaProperties(string schemaNamingContext, string objectType) { string[] data; using (DirectoryEntry de = new DirectoryEntry()) { de.Username = username; de.Password = password; de.Path = "LDAP://" + hostname + "CN=" + objectType + "," + schemaNamingContext; DS.PropertyCollection properties = de.Properties; DS.PropertyValueCollection values = properties["systemMayContain"]; data = new String[values.Count]; values.CopyTo(data, 0); } return data; } Tìm kiếm các đối tượng user Nút tìm kiếm gọi pương thức FillReult(): private void buttonSearch_Click(object sender, System.EventArgs e) { try { FillResult(); } catch (Exception ex) { MessageBox.Show("Check your input: " + ex.Message); } } Ta thiết lập trong FillResult như sau : SearhcScope thiết đặt là subtree,Filter là chuỗi lấy từ textbox và các thuộc tính được tải vào cache được chọn dựa vào listbox: protected void FillResult() { using (DirectoryEntry root = new DirectoryEntry()) { root.Username = username; root.Password = password; root.Path = "LDAP://" + hostname + defaultNamingContext; using (DirectorySearcher searcher = new DirectorySearcher()) { searcher.SearchRoot = root; searcher.SearchScope = SearchScope.Subtree; searcher.Filter = textBoxFilter.Text; searcher.PropertiesToLoad.AddRange(GetProperties()); SearchResultCollection results = searcher.FindAll(); StringBuilder summary = new StringBuilder(); foreach (SearchResult result in results) { foreach (string propName in result.Properties.PropertyNames) { foreach (string s in result.Properties[propName]) { summary.Append(" " + propName + ": " + s + "\r\n"); } } summary.Append("\r\n"); } textBoxResults.Text = summary.ToString(); } } } Kết quả cho ta tất cả các đối tượng được lọc : Code for Download : UserSearch  . domain controller, uesrname ,password để truy nhập Active Directory. ta sẽ truy nhập vào schema của Active Directory để lấy các thuộc tính của đối tượng user các thuộc tính trong schema để trình bày nó. Trong phương thức buttonLoActive DirectoryProperties_Click() ,SetLogonInformation() đọc username,password

Ngày đăng: 21/01/2014, 16:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan