public ResultPropertyCollectionWrapper(ResultPropertyCollection resultPropertyCollection) { if(resultPropertyCollection == null) throw new ArgumentNullException("resultPropertyCollection"); this._resultPropertyCollection = resultPropertyCollection; }
public static string GetSingleStringPropertyCollectionValue(ResultPropertyCollection props, string name) { try { if (!props.Contains(name)) { return string.Empty; } ResultPropertyValueCollection pvc = props[name]; if (pvc == null || pvc.Count == 0) { return string.Empty; } if (string.Compare(name, Constants.Properties.AdProperties.ObjectSID) == 0) { byte[] sidInBytes = (byte[])pvc[0]; SecurityIdentifier sid = new SecurityIdentifier(sidInBytes, 0); return Convert.ToString(sid); } else { return Convert.ToString(pvc[0]); } } catch (Exception ex) { throw new ApplicationException(string.Format("Failed to retrieve property '{0}' from ResultPropertyCollection.", name), ex); } }
private void InitBlock() { _Properties=null; _Entry=null; _PropsToLoad=null; ispropnull=true; _Rproperties=null; }
private static string GetSingleStringPropertyCollectionValue(System.DirectoryServices.ResultPropertyCollection props, string name) { if (!props.Contains(name)) { return(string.Empty); } ResultPropertyValueCollection pvc = props[name]; if (pvc == null || pvc.Count == 0) { return(string.Empty); } return(pvc[0] as string); }
private static string GetObjectSid(System.DirectoryServices.ResultPropertyCollection props, string name) { if (!props.Contains(name)) { return(string.Empty); } ResultPropertyValueCollection pvc = props[name]; if (pvc == null || pvc.Count == 0) { return(string.Empty); } byte[] sidInBytes = (byte[])pvc[0]; SecurityIdentifier sid = new SecurityIdentifier(sidInBytes, 0); return(sid.ToString()); }
public List <string> GetAttributes() { var Attributes = new List <string>(); using (var entry = new DirectoryEntry()) { entry.Path = $"LDAP://{Domain}"; entry.AuthenticationType = AuthenticationTypes.Secure; using (var search = new DirectorySearcher(entry)) { search.Filter = $"(sAMAccountName={UserName})"; SearchResult result = search.FindOne(); System.DirectoryServices.ResultPropertyCollection prop = result.Properties; foreach (object theProperty in prop.PropertyNames) { Attributes.Add(theProperty.ToString() + " = " + result.Properties[theProperty.ToString()][0].ToString() + Environment.NewLine); } } } return(Attributes); }
/// <summary> /// This method retrieves the set of users from active directory that meet the criteria specified in filter. /// </summary> /// <param name="sFilter"></param> /// <returns></returns> /// <remarks></remarks> public static List <string> FetchContacts(string sFilter = "") { // Exceptions are handled by the caller using (DirectorySearcher oSearcher = Initialize()) { if (oSearcher != null) { List <string> cNames = new List <string>(); // Specify what we are looking for, which is the account name of the specified user without any domain information oSearcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user){0})", sFilter); // get all of the matching records using (SearchResultCollection cResults = oSearcher.FindAll()) { if (cResults != null) { foreach (SearchResult theCurrentResult in cResults) { System.DirectoryServices.ResultPropertyCollection oProperties = null; oProperties = theCurrentResult.Properties; // First, verify that at least the display name is contained in the result if (oProperties.Contains(PROPERTY_DISPLAY_NAME) && oProperties[PROPERTY_DISPLAY_NAME].Count > 0) { cNames.Add(oProperties[PROPERTY_DISPLAY_NAME][0].ToString()); } } } } return(cNames); } } return(null); }
public static string GetSingleStringPropertyCollectionValue(ResultPropertyCollection props, string name) { if (!props.Contains(name)) { return string.Empty; } ResultPropertyValueCollection pvc = props[name]; if (pvc == null || pvc.Count == 0) { return string.Empty; } if (string.Compare(name, Constants.Properties.AdProperties.ObjectSID) == 0) { byte[] sidInBytes = (byte[])pvc[0]; SecurityIdentifier sid = new SecurityIdentifier(sidInBytes, 0); return Convert.ToString(sid); } else { return pvc[0] as string; } }
/// <summary> /// Ctor for an Exchange user from an Active Directory ResultSet /// </summary> /// <param name="properties">Active Directory propertes</param> public ExchangeUser(ResultPropertyCollection properties) { this.accountName = GetFirstString(properties["sAMAccountName"]); this.mailnickname = GetFirstString(properties["mailnickname"]); this.proxyAddresses = GetPrimarySmtpAddress( properties ); this.email = GetFirstString(properties["mail"]); this.commonName = GetFirstString(properties["CN"]); this.legacyExchangeDN = GetFirstString( properties[ "legacyExchangeDN" ] ); this.DN = GetFirstString(properties["distinguishedName"]); this.objectClasses = GetStringList(properties["objectclass"]); this.displayName = GetFirstString(properties["displayName"]); this.freeBusyCommonName = parseFreeBusyCommonName(legacyExchangeDN); this.busyTimesBlocks = new FreeBusyCollection(); AdjustAccountName(); Validate(); }
internal dSPropertyCollection(ResultPropertyCollection rp) { _rp = rp; }
public static ResultPropertyCollectionWrapper FromResultPropertyCollection(ResultPropertyCollection resultPropertyCollection) { return resultPropertyCollection; }
private string GetPrimarySmtpAddress(ResultPropertyCollection coll) { if ( coll == null || coll.Count == 0 ) return null; foreach( string address in coll[ "ProxyAddresses" ] ) { if ( address.StartsWith( "SMTP:" ) ) return address; } return null; }
public static LDAPProperty Convert_LDAPProperty(System.DirectoryServices.ResultPropertyCollection Properties) { var ObjectProperties = new LDAPProperty(); foreach (string propName in Properties.PropertyNames) { if (string.Compare(propName, @"adspath", StringComparison.OrdinalIgnoreCase) != 0) { if (string.Compare(propName, @"objectsid", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(propName, @"sidhistory", StringComparison.OrdinalIgnoreCase) == 0) { // convert all listed sids (i.e. if multiple are listed in sidHistory) var values = new List <string>(); foreach (var property in Properties[propName]) { var sid = new System.Security.Principal.SecurityIdentifier(property as byte[], 0); values.Add(sid.Value); } if (string.Compare(propName, @"objectsid", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.objectsid = values.ToArray(); } else { ObjectProperties.sidhistory = values.ToArray(); } } else if (string.Compare(propName, @"grouptype", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.grouptype = (GroupType)Properties[propName][0]; } else if (string.Compare(propName, @"samaccounttype", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.samaccounttype = (SamAccountType)Properties[propName][0]; } else if (string.Compare(propName, @"objectguid", StringComparison.OrdinalIgnoreCase) == 0) { // convert the GUID to a string ObjectProperties.objectguid = new Guid(Properties[propName][0] as byte[]).ToString(); } else if (string.Compare(propName, @"useraccountcontrol", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.useraccountcontrol = (UACEnumValue)Properties[propName][0]; } else if (string.Compare(propName, @"ntsecuritydescriptor", StringComparison.OrdinalIgnoreCase) == 0) { // $ObjectProperties[$_] = New-Object Security.AccessControl.RawSecurityDescriptor -ArgumentList $Properties[$_][0], 0 var Descriptor = new System.Security.AccessControl.RawSecurityDescriptor(Properties[propName][0] as byte[], 0); if (Descriptor.Owner != null) { ObjectProperties.Owner = Descriptor.Owner; } if (Descriptor.Group != null) { ObjectProperties.Group = Descriptor.Group; } if (Descriptor.DiscretionaryAcl != null) { ObjectProperties.DiscretionaryAcl = Descriptor.DiscretionaryAcl; } if (Descriptor.SystemAcl != null) { ObjectProperties.SystemAcl = Descriptor.SystemAcl; } } else if (string.Compare(propName, @"accountexpires", StringComparison.OrdinalIgnoreCase) == 0) { if ((long)Properties[propName][0] >= DateTime.MaxValue.Ticks) { ObjectProperties.accountexpires = "NEVER"; } else { ObjectProperties.accountexpires = DateTime.FromFileTime((long)Properties[propName][0]); } } else if (string.Compare(propName, @"lastlogon", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(propName, @"lastlogontimestamp", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(propName, @"pwdlastset", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(propName, @"lastlogoff", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(propName, @"badPasswordTime", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(propName, @"whencreated", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(propName, @"whenchanged", StringComparison.OrdinalIgnoreCase) == 0) { DateTime dt; // convert timestamps if (Properties[propName][0] is System.MarshalByRefObject) { // if we have a System.__ComObject var Temp = Properties[propName][0]; var High = (Int32)Temp.GetType().InvokeMember("HighPart", System.Reflection.BindingFlags.GetProperty, null, Temp, null); var Low = (Int32)Temp.GetType().InvokeMember("LowPart", System.Reflection.BindingFlags.GetProperty, null, Temp, null); dt = DateTime.FromFileTime(Int64.Parse(string.Format("0x{0:x8}{1:x8}", High, Low))); } if (Properties[propName][0] is System.DateTime) { dt = (DateTime)Properties[propName][0]; } else { // otherwise just a string dt = DateTime.FromFileTime((long)Properties[propName][0]); } if (string.Compare(propName, @"lastlogon", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.lastlogon = dt; } else if (string.Compare(propName, @"lastlogontimestamp", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.lastlogontimestamp = dt; } else if (string.Compare(propName, @"pwdlastset", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.pwdlastset = dt; } else if (string.Compare(propName, @"lastlogoff", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.lastlogoff = dt; } else if (string.Compare(propName, @"badPasswordTime", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.badPasswordTime = dt; } else if (string.Compare(propName, @"whencreated", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.whencreated = dt; } else if (string.Compare(propName, @"whenchanged", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.whenchanged = dt; } } else if (string.Compare(propName, @"name", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.name = Properties[propName][0] as string; } else if (string.Compare(propName, @"distinguishedname", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.distinguishedname = Properties[propName][0] as string; } else if (string.Compare(propName, @"dnsrecord", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.dnsrecord = Properties[propName][0]; } else if (string.Compare(propName, @"samaccountname", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.samaccountname = Properties[propName][0] as string; } else if (string.Compare(propName, @"member", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.member = Properties[propName].GetValues <string>().ToArray(); } else if (string.Compare(propName, @"memberof", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.memberof = Properties[propName].GetValues <string>().ToArray(); } else if (string.Compare(propName, @"cn", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.cn = Properties[propName].GetValues <string>().ToArray(); } else if (string.Compare(propName, @"objectclass", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.objectclass = Properties[propName].GetValues <string>().ToArray(); } else if (string.Compare(propName, @"managedby", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.managedby = Properties[propName][0] as string; } else if (string.Compare(propName, @"siteobject", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.siteobject = Properties[propName][0] as string; } else if (string.Compare(propName, @"ServicePrincipalName", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.ServicePrincipalName = Properties[propName][0] as string; } else if (string.Compare(propName, @"dnshostname", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.dnshostname = Properties[propName][0] as string; } else if (string.Compare(propName, @"gplink", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.gplink = Properties[propName][0] as string; } else if (string.Compare(propName, @"gpoptions", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.gpoptions = (int)Properties[propName][0]; } else if (string.Compare(propName, @"displayname", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.displayname = Properties[propName][0] as string; } else if (string.Compare(propName, @"path", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.path = Properties[propName][0] as string; } else if (string.Compare(propName, @"siteobjectbl", StringComparison.OrdinalIgnoreCase) == 0) { ObjectProperties.siteobjectbl = Properties[propName][0] as string; } else if (Properties[propName][0] is System.MarshalByRefObject) { // try to convert misc com objects var Prop = Properties[propName]; try { var Temp = Properties[propName][0]; var High = (Int32)Temp.GetType().InvokeMember("HighPart", System.Reflection.BindingFlags.GetProperty, null, Temp, null); var Low = (Int32)Temp.GetType().InvokeMember("LowPart", System.Reflection.BindingFlags.GetProperty, null, Temp, null); ObjectProperties.others.Add(propName, Int64.Parse(string.Format("0x{0:x8}{1:x8}", High, Low))); } catch (Exception e) { Logger.Write_Verbose($@"[Convert-LDAPProperty] error: {e}"); ObjectProperties.others.Add(propName, Prop[0]); } } else if (Properties[propName].Count == 1) { ObjectProperties.others.Add(propName, Properties[propName][0]); } else { ObjectProperties.others.Add(propName, Properties[propName]); } } } return(ObjectProperties); }
/// <summary> /// /// </summary> /// <param name="resultPropertyCollection"> /// </param> public ResultPropertyCollectionWrap(ResultPropertyCollection resultPropertyCollection) { this.resultPropertyCollection = resultPropertyCollection; this.PropertyNames = this.resultPropertyCollection.PropertyNames; this.Values = this.resultPropertyCollection.Values; }
public AuthenticatedUser(ResultPropertyCollection resultProperties) { foreach (string key in resultProperties.PropertyNames) { // key is (apparently) always lowercased foreach (object values in resultProperties[key]) { switch (key) { case "objectclass": if (values.ToString() == "user") { this.Type = PersonType.User; } else if (values.ToString() == "group") { this.Type = PersonType.Group; } else { this.Type = PersonType.Contact; } break; case "displayname": this.DisplayName = Sanitize(values.ToString()); break; case "mail": this.Mail = Sanitize(values.ToString()); break; case "cn": // commonname this.CommonName = Sanitize(values.ToString()); break; case "department": this.Department = Sanitize(values.ToString()); break; case "givenname": this.GivenName = Sanitize(values.ToString()); break; case "samaccountname": this.AccountName = Sanitize(values.ToString()); break; case "c": // country this.Country = Sanitize(values.ToString()); break; case "l": // location this.Location = Sanitize(values.ToString()); break; case "url": // url (could be used for profile pictures) this.Url = Sanitize(values.ToString()); break; case "company": this.Company = Sanitize(values.ToString()); break; case "title": this.Title = Sanitize(values.ToString()); break; /* case "sn": // surname break; case "name": break; case "distinguishedname": break; case "member": break; case "initials": break; case "postalcode": break; case "l": // location break; case "c": break; case "mobile": break; case "homephone": break; case "title": break; case "co": break; case "st": // state break; case "password": break; case "memberof": break; case "uid": break; case "description": break; */ } } } }
private void Initialize(DirectoryEntry domainRoot) { string[] policyAttributes = new string[] { "maxPwdAge", "minPwdAge", "minPwdLength", "lockoutDuration", "lockOutObservationWindow", "lockoutThreshold", "pwdProperties", "pwdHistoryLength", "objectClass", "distinguishedName" }; //we take advantage of the marshaling with //DirectorySearcher for LargeInteger values... DirectorySearcher ds = new DirectorySearcher( domainRoot, "(objectClass=domainDNS)", policyAttributes, SearchScope.Base ); SearchResult result = ds.FindOne(); //do some quick validation... if (result == null) { throw new ArgumentException( "domainRoot is not a domainDNS object." ); } this.attribs = result.Properties; }
/// <summary> /// Parses the properties for a contact that came from AD /// and return GWiseContact object created from those properties. /// If some of the properties are misisng or mallformed /// the function will return null. /// In addition to parsing the contact the function returns the /// URL for the folder that contains the free busy message for /// the account, if it wasn't computed yet. /// </summary> /// <param name="contactProps">The properties of the contact</param> /// <param name="freeBusyUrl">If not already computed, /// it will be set to the free busy folder URL</param> /// <returns>A contact object or null</returns> private static GWiseContact ParseGWiseContactsFromADProperties( ResultPropertyCollection contactProps, ref string freeBusyUrl) { string gwiseUid = null; string gwiseAddress = null; string commonGroup = null; string freeBusyUrlTemp = null; GWiseContact gwiseContact = null; foreach (string propName in contactProps.PropertyNames) { foreach (Object propObject in contactProps[propName]) { string propValue = propObject.ToString(); if ((freeBusyUrl == null) && (freeBusyUrlTemp == null)) { freeBusyUrlTemp = GenerateParentFreeBusyFolderUrl( propName, propValue); } if (gwiseUid == null) { gwiseUid = GetGWiseUidFromLegacyExchangeDN( propName, propValue); } if (commonGroup == null) { commonGroup = GetCommonGroupFromLegacyExchangeDN( propName, propValue); } if (gwiseAddress == null) { gwiseAddress = GetGWiseAddressFromProxyAddresses( propName, propValue); } } } if ((gwiseAddress != null) && (gwiseUid != null) && (commonGroup != null)) { gwiseContact = new GWiseContact(gwiseUid, gwiseAddress, commonGroup); } if ((freeBusyUrl == null) && (gwiseContact != null) && (freeBusyUrlTemp != null)) { // Return the free busy URL if not set already, // but do that only for well formed accounts. freeBusyUrl = freeBusyUrlTemp; } return gwiseContact; }