Пример #1
0
        public bool PasswordChangeRequired()
        {
            IADsLargeInteger lastSetLongInt = (IADsLargeInteger)user.Properties["pwdLastSet"].Value;
            long             filetime       = lastSetLongInt.HighPart * 4294967296 + lastSetLongInt.LowPart;

            return(filetime == 0);
        }
Пример #2
0
        public DateTime PasswordExpiresDate()
        {
            IADsLargeInteger lastSetLongInt  = (IADsLargeInteger)user.Properties["pwdLastSet"].Value;
            long             filetime        = lastSetLongInt.HighPart * 4294967296 + lastSetLongInt.LowPart;
            DateTime         PasswordLastSet = DateTime.FromFileTime(filetime);

            return(PasswordLastSet.AddDays(passwordMaxAge()));
        }
Пример #3
0
        private static long GetLongFromLargeInteger(IADsLargeInteger Li)
        {
            long retval = Li.HighPart;

            retval <<= 32;
            retval  |= (uint)Li.LowPart;
            return(retval);
        }
Пример #4
0
        private static string ConvertToTimeString(DirectoryEntry objRootDSE, string strAttrName, string propertyValue)
        {
            IADsLargeInteger IADstimeObj = (IADsLargeInteger)objRootDSE.Properties[strAttrName].Value;
            long             timeObj     = GetLongFromLargeInteger(IADstimeObj);

            propertyValue = DateTime.FromFileTime(timeObj).ToString();
            return(propertyValue);
        }
Пример #5
0
        public static long IADsLargeIntegerToInt64(IADsLargeInteger value)
        {
            if (value != null)
            {
                return Utils.MakeLong(value.HighPart, value.LowPart);
            }

            return 0;
        }
Пример #6
0
        public static long IADsLargeIntegerToInt64(IADsLargeInteger value)
        {
            if (value != null)
            {
                return(Utils.MakeLong(value.HighPart, value.LowPart));
            }

            return(0);
        }
Пример #7
0
        private static DateTime GetDateTimeFromLargeInteger(IADsLargeInteger largeIntValue)
        {
            if (largeIntValue == null)
            {
                return(DateTime.MinValue);
            }

            long int64Value = (long)((uint)largeIntValue.LowPart + (((long)largeIntValue.HighPart) << 32));

            return(DateTime.FromFileTimeUtc(int64Value));
        }
Пример #8
0
        public static DateTime?IADsLargeIntegerToDateTime(IADsLargeInteger value)
        {
            long fileTime = IADsLargeIntegerToInt64(value);

            if (fileTime != 0)
            {
                return(DateTime.FromFileTime(fileTime));
            }

            return(null);
        }
Пример #9
0
        /// <summary>
        /// Checks to see if the account is locked out in the directory
        /// </summary>
        /// <returns>True if the account is locked out</returns>
        public bool isLockedOutAccount()
        {
            if (this.ActiveDirectoryEntry.Properties["lockoutTime"].Value != null)
            {
                IADsLargeInteger currentValue = (IADsLargeInteger)this.ActiveDirectoryEntry.Properties["lockoutTime"].Value;
                if (GetLongFromLargeInteger(currentValue) != 0)
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #10
0
        public static adComputers adComputerDetailsFinder(PrincipalContext context, string computer)
        {
            adComputers adComputer = new adComputers();

            try
            {
                ComputerPrincipal comp = ComputerPrincipal.FindByIdentity(context, computer);
                if (comp != null)
                {
                    DirectoryEntry deepDetails = comp.GetUnderlyingObject() as System.DirectoryServices.DirectoryEntry;

                    adComputer.Name        = comp.Name;
                    adComputer.DN          = comp.DistinguishedName;
                    adComputer.Description = comp.Description;

                    if (deepDetails.Properties.Contains("operatingSystem"))
                    {
                        adComputer.OS = deepDetails.Properties["operatingSystem"].Value.ToString();
                    }
                    if (deepDetails.Properties.Contains("whenCreated"))
                    {
                        adComputer.CreationDate = deepDetails.Properties["whenCreated"].Value.ToString();
                    }
                    if (deepDetails.Properties.Contains("whenChanged"))
                    {
                        adComputer.ChangedDate = deepDetails.Properties["whenChanged"].Value.ToString();
                    }
                    if (deepDetails.Properties.Contains("ms-Mcs-AdmPwd"))
                    {
                        adComputer.LapPAS = deepDetails.Properties["ms-Mcs-AdmPwd"].Value.ToString();
                    }
                    if (deepDetails.Properties.Contains("ms-Mcs-AdmPwdExpirationTime"))
                    {
                        Int64 lastLogonThisServer = new Int64();
                        try
                        {
                            IADsLargeInteger lgInt = (IADsLargeInteger)deepDetails.Properties["ms-Mcs-AdmPwdExpirationTime"].Value;
                            lastLogonThisServer             = ((long)lgInt.HighPart << 32) + lgInt.LowPart;
                            adComputer.LapPASExpirationDate = (DateTime.FromFileTime(lastLogonThisServer)).ToString();
                        }
                        catch (Exception e)
                        {
                        }
                    }
                }
            }
            catch { Exception e; }
            return(adComputer);
        }
Пример #11
0
 /// <summary>
 /// Format the attribute as a date
 /// </summary>
 /// <param name="oV">the attribute for a active directory object</param>
 /// <returns>returns formatted date value</returns>
 public string AdsDateValue(object oV)
 {
     try
     {
         IADsLargeInteger v     = (IADsLargeInteger)oV;
         long             dV    = Convert.ToInt64(v.HighPart) * 4294967296L + Convert.ToInt64(v.LowPart);
         DateTime         xDate = System.DateTime.FromFileTime(dV);
         return(xDate.ToString("dd-MMM-yyyy h:mm tt").ToUpper());
     }
     catch
     {
         DateTime xDate = System.DateTime.MinValue;
         return(xDate.ToString("dd-MMM-yyyy h:mm tt").ToUpper());
     }
 }
Пример #12
0
        /// <summary>
        /// 解析DirectoryEntry对象。
        /// </summary>
        /// <param name="entry">DirectoryEntry对象。</param>
        protected override void Parse(DirectoryEntry entry)
        {
            base.Parse(entry, SchemaClass.user);        // 调用基类方法

            this.userName           = Utils.GetProperty(entry, User.PROPERTY_ACCOUNT_SAM);
            this.firstName          = Utils.GetProperty(entry, User.PROPERTY_GENERAL_GIVENNAME);
            this.lastName           = Utils.GetProperty(entry, User.PROPERTY_GENERAL_LASTNAME);
            this.initials           = Utils.GetProperty(entry, User.PROPERTY_GENERAL_INITIALS);
            this.displayName        = Utils.GetProperty(entry, User.PROPERTY_GENERAL_DISPLAYNAME);
            this.office             = Utils.GetProperty(entry, User.PROPERTY_GENERAL_OFFICE);
            this.title              = Utils.GetProperty(entry, User.PROPERTY_ORGAN_TITLE);
            this.manager            = Utils.GetProperty(entry, User.PROPERTY_ORGAN_MANAGER);
            this.department         = Utils.GetProperty(entry, User.PROPERTY_ORGAN_DEPARTMENT);
            this.telephone          = Utils.GetProperty(entry, User.PROPERTY_GENERAL_TEL);
            this.mobile             = Utils.GetProperty(entry, User.PROPERTY_TEL_MOBILE);
            this.mail               = Utils.GetProperty(entry, User.PROPERTY_GENERAL_MAIL);
            this.principalName      = Utils.GetProperty(entry, User.PROPERTY_ACCOUNT_PRINCIPAL);
            this.userAccountControl = Convert.ToInt32(Utils.GetProperty(entry, User.PROPERTY_ACCOUNT_CONTROL));

            string primaryGroupIDStr = Utils.GetProperty(entry, User.PROPERTY_MEMBEROF_PRIMARY);

            if (primaryGroupIDStr != null)
            {
                this.primaryGroupID = int.Parse(primaryGroupIDStr);
            }
            else
            {
                this.primaryGroupID = (int?)null;
            }

            IADsLargeInteger li = (IADsLargeInteger)entry.Properties[User.PROPERTY_ACCOUNT_PWDLASTSET][0];

            this.pwdLastSet = (li.HighPart << 0x20) + li.LowPart;

            if (entry.Properties.Contains(User.PROPERTY_MEMBEROF_ALL))
            {
                List <string> ms = new List <string>();
                foreach (object m in entry.Properties[User.PROPERTY_MEMBEROF_ALL])
                {
                    ms.Add(Utils.EscapeDNBackslashedChar(m.ToString()));        // 转义/
                }
                this.memberOf = ms.ToArray();
            }
            else
            {
                this.memberOf = new string[] { }
            };
        }
Пример #13
0
        private static IADsLargeInteger GetLargeIntegerFromDateTime(DateTime dateTimeValue)
        {
            //
            // Convert DateTime value to utc file time
            //
            Int64 int64Value = dateTimeValue.ToFileTimeUtc();

            //
            // convert to large integer
            //
            IADsLargeInteger largeIntValue = (IADsLargeInteger) new LargeInteger();

            largeIntValue.HighPart = (int)(int64Value >> 32);
            largeIntValue.LowPart  = (int)(int64Value & 0xFFFFFFFF);

            return(largeIntValue);
        }
Пример #14
0
class a { static void Main()
          {
//ldap root
              DirectoryEntry de = new DirectoryEntry("LDAP://DC=root,DC=f**k");
              object         o  = null;

//IADsLargeInteger
              o = de.Properties["creationTime"].Value;
              w(o);
              w(o is IADsLargeInteger);
              IADsLargeInteger li = o as IADsLargeInteger;

              w(makelong(li.HighPart, li.LowPart));
              w(DateTime.FromFileTimeUtc(makelong(li.HighPart, li.LowPart)));
//IADsSecurityDescriptor
              o = de.Properties["nTSecurityDescriptor"].Value;
              w(o);
              w(o is IADsSecurityDescriptor);
              IADsSecurityDescriptor id = o as IADsSecurityDescriptor;

              w(id.Group);
              w(id.Owner);
              int ADS_SD_FORMAT_IID       = 1;
              int ADS_SD_FORMAT_RAW       = 2;
              int ADS_SD_FORMAT_HEXSTRING = 3;
              ADsSecurityUtilityClass suc = new ADsSecurityUtilityClass();

              w(suc.ConvertSecurityDescriptor(id, ADS_SD_FORMAT_IID, ADS_SD_FORMAT_RAW));
              ActiveDirectorySecurity adsec = new ActiveDirectorySecurity();

              adsec.SetSecurityDescriptorBinaryForm(suc.ConvertSecurityDescriptor(id, ADS_SD_FORMAT_IID, ADS_SD_FORMAT_RAW) as byte[]);
              w(adsec.GetSecurityDescriptorSddlForm(AccessControlSections.All));
              w(suc.ConvertSecurityDescriptor(id, ADS_SD_FORMAT_IID, ADS_SD_FORMAT_HEXSTRING));
//IADsDNWithBinary
              o = de.Properties["wellKnownObjects"].Value;
              w(o);
              o = (de.Properties["wellKnownObjects"].Value as object[])[0];
              w(o);
              w(o is IADsDNWithBinary);
              IADsDNWithBinary dnb = o as IADsDNWithBinary;

              w(dnb.DNString);
//IADsDNWithString
//与IADsDNWithBinary代码类似,但未找到此类型的属性
          }
        protected override object OnGetValue()
        {
            try
            {
                object value = Entry.InvokeGet(_attributeName);

                IADsLargeInteger largeInteger = value as IADsLargeInteger;

                if (largeInteger != null)
                {
                    return(DirectoryUtils.IADsLargeIntegerToDateTime(largeInteger));
                }

                return(value);
            }
            catch (Exception exc)
            {
                return(exc);
            }
        }
Пример #16
0
        public string GetAccountExpires(string format = "")
        {
            IADsLargeInteger lgInt   = (IADsLargeInteger)User.Properties["accountExpires"].Value;
            long             expires = ((long)lgInt.HighPart << 32) + lgInt.LowPart;

            // the values 0 and (2^63 - 1) mean "Never"
            if (expires == 0 || expires == 9223372032559808511)
            {
                return("Never");
            }
            else
            {
                try
                {
                    return(DateTime.FromFileTime(expires).ToString(format));
                }
                catch (ArgumentOutOfRangeException)
                {
                    return("error");
                }
            }
        }
Пример #17
0
        /// <summary>
        /// Assigns the specified property on the specified object based on the data wrapped in the DirectoryEntry representing the current result.
        /// </summary>
        /// <param name="helper">Active Directory helper type to help retrieving the target value.</param>
        /// <param name="searchResult">SearchResult object containing the data for the current result.</param>
        /// <param name="result">Object the property has to be set on.</param>
        /// <param name="prop">Property to be set.</param>
        private void AssignResultProperty(Type helper, SearchResult searchResult, object result, string prop)
        {
            PropertyInfo i = m_originalType.GetProperty(prop);

            DirectoryAttributeAttribute[] da = i.GetCustomAttributes(typeof(DirectoryAttributeAttribute), false) as DirectoryAttributeAttribute[];
            if (da != null && da.Length != 0)
            {
                if (da[0].QuerySource == DirectoryAttributeType.ActiveDs)
                {
                    PropertyInfo p = helper.GetProperty(da[0].Attribute, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    try
                    {
                        i.SetValue(result, p.GetValue(searchResult.GetDirectoryEntry().NativeObject, null), null);
                    }
                    catch (TargetInvocationException) { }
                }
                else
                {
                    DirectoryEntry e = searchResult.GetDirectoryEntry();

                    var resultValue = e.Properties[da[0].Attribute];
                    if (i.PropertyType.IsArray)
                    {
                        //
                        // Byte array properties are special in AD. Here we don't follow the
                        // heuristic of an array-typed property to be an expanded multi-value
                        // property but we support splatting the contents of the value into
                        // the byte[] array.
                        //
                        byte[] value;
                        if (i.PropertyType.GetElementType() == typeof(byte) && (value = resultValue[0] as byte[]) != null)
                        {
                            i.SetValue(result, value, null);
                        }
                        else
                        {
                            Array o = Array.CreateInstance(i.PropertyType.GetElementType(), resultValue.Count);

                            int j = 0;
                            foreach (object oo in resultValue)
                            {
                                o.SetValue(oo, j++);
                            }

                            i.SetValue(result, o, null);
                        }
                    }
                    else
                    {
                        if (resultValue.Count == 1)
                        {
                            //
                            // Support GUID field mapping.
                            //
                            if (i.PropertyType == typeof(Guid))
                            {
                                byte[] value = resultValue[0] as byte[];
                                if (value == null)
                                {
                                    throw new NotSupportedException("Mapping of Guid-typed property " + i.Name + " to non-byte[] valued directory field " + da[0].Attribute + ".");
                                }

                                i.SetValue(result, new Guid(value), null);
                            }
                            else if (i.PropertyType == typeof(DateTime))
                            {
                                IADsLargeInteger largeIntValue = (IADsLargeInteger)resultValue[0];
                                long             int64Value    = (long)((uint)largeIntValue.LowPart + (((long)largeIntValue.HighPart) << 32));
                                i.SetValue(result, DateTime.FromFileTime(int64Value), null);
                            }
                            else
                            {
                                i.SetValue(result, resultValue[0], null);
                            }
                        }
                    }
                }
            }
            else
            {
                var pvc = searchResult.GetDirectoryEntry().Properties[prop];
                if (pvc.Count == 1)
                {
                    i.SetValue(result, pvc[0], null);
                }
            }
        }
Пример #18
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="aEntry"></param>
        /// <param name="aPropEntry"></param>
        /// <param name="aADsType"></param>
        /// <returns></returns>
        public string PropertyToString(DirectoryEntry aEntry, IADsPropertyEntry aPropEntry, int aADsType)
        {
            List <string> alsValues = new List <string>();
            ADSTYPEENUM   oEnum     = (ADSTYPEENUM)aADsType;

            try
            {
                foreach (IADsPropertyValue oPropValue in (object[])aPropEntry.Values)
                {
                    switch (oEnum)
                    {
                    case ADSTYPEENUM.ADSTYPE_DN_STRING:
                        alsValues.Add(oPropValue.DNString);
                        break;

                    case ADSTYPEENUM.ADSTYPE_CASE_EXACT_STRING:
                        alsValues.Add(oPropValue.CaseExactString);
                        break;

                    case ADSTYPEENUM.ADSTYPE_CASE_IGNORE_STRING:
                        alsValues.Add(oPropValue.CaseIgnoreString);
                        break;

                    case ADSTYPEENUM.ADSTYPE_PRINTABLE_STRING:
                        alsValues.Add(oPropValue.PrintableString);
                        break;

                    case ADSTYPEENUM.ADSTYPE_NUMERIC_STRING:
                        alsValues.Add(oPropValue.NumericString);
                        break;

                    case ADSTYPEENUM.ADSTYPE_BOOLEAN:
                        alsValues.Add(oPropValue.Boolean.ToString());
                        break;

                    case ADSTYPEENUM.ADSTYPE_INTEGER:
                        if (aPropEntry.Name == "primaryGroupID")
                        {
                            alsValues.Add(oPropValue.Integer.ToString() + m_Delimiter + "(" + GetPrimaryGroup(aEntry, null) + ")");
                        }
                        else
                        {
                            int iValue = oPropValue.Integer;
                            alsValues.Add(iValue.ToString() + m_Delimiter + "(Hex: 0x" + iValue.ToString("X") + ")");
                        }
                        break;

                    case ADSTYPEENUM.ADSTYPE_UTC_TIME:
                        alsValues.Add(oPropValue.UTCTime.ToString());
                        break;

                    case ADSTYPEENUM.ADSTYPE_OCTET_STRING:
                        // handle the objectSID
                        if (aPropEntry.Name == "objectSid")
                        {
                            // alsValues.Add(oPropValue.OctetString.ToString());
                            StringBuilder sbSID  = new StringBuilder();
                            byte[]        usrSID = (byte[])oPropValue.OctetString;

                            foreach (byte b in usrSID)
                            {
                                sbSID.AppendFormat("{0:x2}", b);
                            }

                            alsValues.Add(sbSID.ToString() + m_Delimiter + "(" + ObjectSidToString(usrSID) + ")");
                        }
                        else if (aPropEntry.Name == "objectGUID")
                        {
                            StringBuilder sbGUID  = new StringBuilder();
                            byte[]        usrGUID = (byte[])oPropValue.OctetString;

                            foreach (byte b in usrGUID)
                            {
                                sbGUID.AppendFormat("{0:x2}", b);
                            }

                            alsValues.Add(sbGUID.ToString() + m_Delimiter + "(CLSID-style: " + ObjectGUIDToStringGUID(sbGUID.ToString()) + ")");
                        }
                        else
                        {
                            alsValues.Add("(OCTET_STRING not yet supported)");
                        }
                        break;

                    case ADSTYPEENUM.ADSTYPE_LARGE_INTEGER:
                        try
                        {
                            Int64            iLargeInt = 0;
                            IADsLargeInteger int64Val  = (IADsLargeInteger)oPropValue.LargeInteger;
                            iLargeInt = int64Val.HighPart * 4294967296 + int64Val.LowPart;

                            // check for special cases
                            string sPropName = aPropEntry.Name.ToLower();
                            if (sPropName == "accountexpires" || sPropName == "badpasswordtime" || sPropName == "creationtime" ||
                                sPropName == "lastlogoff" || sPropName == "lastlogon" || sPropName == "pwdlastset")
                            {
                                string sTimeString = ConvertLargeIntToDate(iLargeInt);
                                alsValues.Add(sTimeString + m_Delimiter + iLargeInt.ToString());
                            }
                            else
                            {
                                alsValues.Add(iLargeInt.ToString() + m_Delimiter + "(Hex: 0x" + iLargeInt.ToString("X") + ")");
                            }
                        }
                        catch (Exception)
                        {
                            //
                        }

                        break;

                    case ADSTYPEENUM.ADSTYPE_CASEIGNORE_LIST:
                        alsValues.Add("(CASEIGNORE_LIST not yet supported)");
                        break;

                    case ADSTYPEENUM.ADSTYPE_PATH:
                        alsValues.Add("(PATH not yet supported)");
                        break;

                    case ADSTYPEENUM.ADSTYPE_INVALID:
                        alsValues.Add("(invalid ADs type: " + aADsType.ToString() + ")");
                        break;

                    case ADSTYPEENUM.ADSTYPE_PROV_SPECIFIC:
                        alsValues.Add("(PROV_SPECIFIC not yet supported)");
                        break;

                    case ADSTYPEENUM.ADSTYPE_OBJECT_CLASS:
                        alsValues.Add("(OBJECT_CLASS not yet supported)");
                        break;

                    case ADSTYPEENUM.ADSTYPE_OCTET_LIST:
                        alsValues.Add("(OCTET_LIST not yet supported)");
                        break;

                    case ADSTYPEENUM.ADSTYPE_POSTALADDRESS:
                        alsValues.Add("(POSTALADDRESS not yet supported)");
                        break;

                    case ADSTYPEENUM.ADSTYPE_TIMESTAMP:
                        alsValues.Add("(TIMESTAMP not yet supported)");
                        break;

                    case ADSTYPEENUM.ADSTYPE_BACKLINK:
                        alsValues.Add("(BACKLINK not yet supported)");
                        break;

                    case ADSTYPEENUM.ADSTYPE_TYPEDNAME:
                        alsValues.Add("(TYPEDNAME not yet supported)");
                        break;

                    case ADSTYPEENUM.ADSTYPE_HOLD:
                        alsValues.Add("(HOLD not yet supported)");
                        break;

                    case ADSTYPEENUM.ADSTYPE_NETADDRESS:
                        alsValues.Add("(NETADDRESS not yet supported)");
                        break;

                    case ADSTYPEENUM.ADSTYPE_REPLICAPOINTER:
                        alsValues.Add("(REPLICAPOINTER not yet supported)");
                        break;

                    case ADSTYPEENUM.ADSTYPE_FAXNUMBER:
                        alsValues.Add("(FAXNUMBER not yet supported)");
                        break;

                    case ADSTYPEENUM.ADSTYPE_EMAIL:
                        alsValues.Add("(EMAIL not yet supported)");
                        break;

                    case ADSTYPEENUM.ADSTYPE_NT_SECURITY_DESCRIPTOR:
                        alsValues.Add(aADsType.ToString());
                        break;

                    case ADSTYPEENUM.ADSTYPE_UNKNOWN:
                        alsValues.Add("(unknown AD type: " + aADsType.ToString() + ")");
                        break;

                    case ADSTYPEENUM.ADSTYPE_DN_WITH_BINARY:
                        alsValues.Add("(DN_WITH_BINARY not yet supported)");
                        break;

                    case ADSTYPEENUM.ADSTYPE_DN_WITH_STRING:
                        alsValues.Add("(DN_WITH_STRING not yet supported)");
                        break;

                    default:
                        alsValues.Add("(unknown AD type: " + aADsType.ToString() + ")");
                        break;
                    }
                }
            }
            catch { }

            if (alsValues.Count > 0)
            {
                StringBuilder oSB = new StringBuilder(alsValues[0].ToString());
                for (int ix = 1; ix < alsValues.Count; ix++)
                {
                    oSB.Append(m_Delimiter + alsValues[ix]);
                }

                return(oSB.ToString());
            }
            else
            {
                return(string.Empty);
            }
        }
 private long GetTimeValue(IADsLargeInteger largeInteger)
 {
     return((long)largeInteger.LowPart | (long)(largeInteger.HighPart << 32));
 }
Пример #20
0
        public static DateTime? IADsLargeIntegerToDateTime(IADsLargeInteger value)
        {
            long fileTime = IADsLargeIntegerToInt64(value);

            if (fileTime != 0)
            {
                return DateTime.FromFileTime(fileTime);
            }

            return null;
        }
Пример #21
0
 internal static long GetLongValue(IADsLargeInteger value)
 {
     return((long)(((ulong)value.HighPart << 32) + (ulong)value.LowPart));
 }
Пример #22
0
        public static long GetPropertyValueAsInt64(DirectoryEntry entry, string property)
        {
            IADsLargeInteger value = GetPropertyValue(entry, property) as IADsLargeInteger;

            return(IADsLargeIntegerToInt64(value));
        }