private ADInfoModel GetUserInfoTask(object obj) { string account = obj as string; ADInfoModel result = null; try { string path = string.Format("LDAP://{0}", this.Domain); string domainaccount = string.Format("{0}\\{1}", this.Domain, this.Account); using (DirectoryEntry entry = new DirectoryEntry(path, domainaccount, this.Password)) { using (DirectorySearcher search = new DirectorySearcher(entry)) { search.Filter = "(&(&(objectCategory=person)(objectClass=user))(sAMAccountName=" + account + "))"; search.SearchScope = SearchScope.Subtree; SearchResult searchResult = search.FindOne(); if (searchResult != null) { result = Get(searchResult); } } } } catch (Exception ex) { this.OnErrorCall(ex); } return(result); }
private ADInfoModel Get(SearchResult searchResult) { ADInfoModel m = null; try { if (searchResult != null) { m = new ADInfoModel(); m.Properties = new ADPropertyCollection(searchResult.Properties.Count); foreach (DictionaryEntry kv in searchResult.Properties) { if (kv.Key != null && kv.Value is ResultPropertyValueCollection) { string name = kv.Key.ToString(); object obj = this.GetValue(kv.Value as ResultPropertyValueCollection); string val = null; if (string.Compare(name, "objectguid") == 0 && obj is byte[]) { Guid guid = new Guid(obj as byte[]); m.Guid = guid; val = guid.ToString(); } else { val = GetString(obj); } if (!string.IsNullOrEmpty(val)) { m.Properties[name] = val; } } } m.Path = searchResult.Path; if (m.Properties.Contains("name")) { m.Name = m.Properties["name"]; } if (m.Properties.Contains("objectclass")) { m.SchemaClassName = m.Properties["objectclass"]; } } } catch { m = null; } return(m); }
/// <summary> /// 获取指定域账号信息 /// </summary> /// <param name="account">域账号</param> /// <param name="millisecondsTimeout">超时时间(毫秒)</param> /// <returns>域账号信息</returns> public ADInfoModel GetUserInfo(string account, int millisecondsTimeout) { ADInfoModel model = null; var task = Task.Factory.StartNew(new Func <object, ADInfoModel>(this.GetUserInfoTask), account); task.Wait(millisecondsTimeout); if (task.IsCompleted) { model = task.Result; } return(model); }
/// <summary> /// 获取指定域账号信息 /// </summary> /// <param name="account">域账号</param> /// <param name="millisecondsTimeout">超时时间(毫秒)</param> /// <returns>域账号信息</returns> public ADInfoModel GetUserInfo(string account, int millisecondsTimeout) { if (string.IsNullOrEmpty(this.Account)) { throw new ArgumentNullException("ADDomain.Account"); } if (string.IsNullOrEmpty(this.Password)) { throw new ArgumentNullException("ADDomain.Password"); } ADInfoModel model = null; var task = Task.Factory.StartNew(new Func <object, ADInfoModel>(this.GetUserInfoTask), account); if (task.Wait(millisecondsTimeout) || task.IsCompleted) { model = task.Result; } return(model); }