Example #1
0
        void Process(string ou, DirectoryEntry de)
        {
            string name = de.Name.StripCNFromName();

              try
              {
            if (de.SchemaClassName.Equals("user") || de.SchemaClassName.Equals("computer"))
            {
              #region Get the data out of the Directory Entry
              ADInfo info = new ADInfo();
              info.name = name;
              info.sAMAccountName = ad.GetStringProperty(de, "sAMAccountName");
              info.mail = ad.GetStringProperty(de, "mail");
              info.displayName = ad.GetStringProperty(de, "displayName");
              info.mailNickname = ad.GetStringProperty(de, "mailNickName");
              info.company = ad.GetStringProperty(de, "company");
              info.c = ad.GetStringProperty(de, "c");
              info.department = ad.GetStringProperty(de, "department");
              info.facsimileTelephoneNumber = ad.GetStringProperty(de, "facsimileTelephoneNumber");
              info.givenName = ad.GetStringProperty(de, "givenName");
              info.initials = ad.GetStringProperty(de, "initials");
              info.sn = ad.GetStringProperty(de, "sn");
              info.adminDescription = ad.GetStringProperty(de, "adminDescription");
              info.physicalDeliveryOfficeName = ad.GetStringProperty(de, "physicalDeliveryOfficeName");
              info.telephoneNumber = ad.GetStringProperty(de, "telephoneNumber");
              info.title = ad.GetStringProperty(de, "title");
              info.homeMTA = ad.GetStringProperty(de, "homeMTA");
              info.homeMDB = ad.GetStringProperty(de, "homeMDB");
              info.whenCreated = ad.GetStringProperty(de, "whenCreated");
              info.whenChanged = ad.GetStringProperty(de, "whenChanged");
              info.msExchHideFromAddressLists = ad.GetStringProperty(de, "msExchHideFromAddressLists");
              info.publicDelegatesBL = null;

              #region publicDelegatesBL
              if ((de.Properties["publicDelegatesBL"] as PropertyValueCollection).Count > 0)
              {
            StringBuilder pd = new StringBuilder();

            PropertyValueCollection pvc = (de.Properties["publicDelegatesBL"] as PropertyValueCollection);

            for (int i = 0; i < pvc.Count; i++)
            {
              string delim = String.Empty;

              if (i < (pvc.Count - 1)) delim = ";";

              pd.Append(String.Format("{0}{1}", pvc[i].ToString(), delim));
            }

            if (pd.Length > 0) info.publicDelegatesBL = pd.ToString();
              }
              #endregion

              #region userSMIMECertificate and userCertificate
              if (de.SchemaClassName.Equals("user"))
              {
            if (de.Properties.Contains("userSMIMECertificate"))
            {
              info.userSMIMECertificate = "True";

              X509Certificate2 x509 = new X509Certificate2(de.Properties["userSMIMECertificate"].Value as byte[]);

              info.certificateValidFromDate = x509.NotBefore.ToString();
              info.certificateExpirationDate = x509.NotAfter.ToString();
              info.certificateEmailAddress = x509.GetNameInfo(X509NameType.EmailName, false);

              x509 = null;
            }
            else
            {
              info.userSMIMECertificate = "False";

              if (de.Properties.Contains("userCertificate"))
              {
                info.userCertificate = "True";

                try
                {
                  X509Certificate2 x509 = new X509Certificate2(de.Properties["userCertificate"].Value as byte[]);

                  info.certificateValidFromDate = x509.NotBefore.ToString();
                  info.certificateExpirationDate = x509.NotAfter.ToString();
                  info.certificateEmailAddress = x509.GetNameInfo(X509NameType.EmailName, false);

                  x509 = null;
                }
                catch { }
              }
              else
                info.userCertificate = "False";
            }
              }
              #endregion

              info.altRecipient = ad.GetStringProperty(de, "altRecipient");
              info.legacyExchangeDN = ad.GetStringProperty(de, "legacyExchangeDN");
              info.MSExchangeMailboxGUID = ad.GetStringProperty(de, "MSExchangeMailboxGUID");
              info.userPrincipalName = ad.GetStringProperty(de, "userPrincipalName");
              info.distinguishedName = ad.GetStringProperty(de, "distinguishedName");
              info.homeDirectory = ad.GetStringProperty(de, "homeDirectory");
              info.scriptPath = ad.GetStringProperty(de, "scriptPath");
              info.objectCategory = ad.GetStringProperty(de, "objectCategory");
              info.msExchHomeServerName = ad.GetStringProperty(de, "msExchHomeServerName");
              info.mDBStorageQuota = ad.GetStringProperty(de, "mDBStorageQuota");
              info.mDBOverQuotaLimit = ad.GetStringProperty(de, "mDBOverQuotaLimit");
              info.mDBOverHardQuotaLimit = ad.GetStringProperty(de, "mDBOverHardQuotaLimit");
              info.objectClass = de.SchemaClassName;
              info.extensionAttribute1 = ad.GetStringProperty(de, "extensionAttribute1");
              info.extensionAttribute2 = ad.GetStringProperty(de, "extensionAttribute2");
              info.extensionAttribute3 = ad.GetStringProperty(de, "extensionAttribute3");
              info.extensionAttribute4 = ad.GetStringProperty(de, "extensionAttribute4");
              info.extensionAttribute5 = ad.GetStringProperty(de, "extensionAttribute5");
              info.extensionAttribute6 = ad.GetStringProperty(de, "extensionAttribute6");
              info.extensionAttribute7 = ad.GetStringProperty(de, "extensionAttribute7");
              info.extensionAttribute8 = ad.GetStringProperty(de, "extensionAttribute8");
              info.extensionAttribute9 = ad.GetStringProperty(de, "extensionAttribute9");
              info.extensionAttribute10 = ad.GetStringProperty(de, "extensionAttribute10");
              info.extensionAttribute11 = ad.GetStringProperty(de, "extensionAttribute11");
              info.extensionAttribute12 = ad.GetStringProperty(de, "extensionAttribute12");
              info.extensionAttribute13 = ad.GetStringProperty(de, "extensionAttribute13");
              info.extensionAttribute14 = ad.GetStringProperty(de, "extensionAttribute14");
              info.extensionAttribute15 = ad.GetStringProperty(de, "extensionAttribute15");

              info.lastLogonTimeStamp = ad.GetLastLogonTimeStamp(de);

              #region proxyAddresses
              info.proxyAddresses = null;

              if (de.SchemaClassName.Equals("user"))
              {
            StringBuilder pb = new StringBuilder();
            List<string> proxies = ad.GetProxyAddresses(de);

            for (int i = 0; i < proxies.Count; i++)
            {
              string delim = String.Empty;

              if (i < (proxies.Count - 1)) delim = ";";

              pb.Append(String.Format("{0}{1}", proxies[i].Trim(), delim));
            }

            if (pb.Length > 0) info.proxyAddresses = pb.ToString();
              }
              #endregion

              #region showInAddressBook
              info.showInAddressBook = null;

              if (de.SchemaClassName.Equals("user"))
              {
            StringBuilder ab = new StringBuilder();
            List<string> addr = ad.GetShowInAddressBook(de);

            for (int i = 0; i < addr.Count; i++)
            {
              string delim = String.Empty;

              if (i < (addr.Count - 1)) delim = ";";

              ab.Append(String.Format("{0}{1}", addr[i].Trim(), delim));
            }

            if (ab.Length > 0) info.showInAddressBook = ab.ToString();
              }
              #endregion

              #region memberOf
              StringBuilder mb = new StringBuilder();
              List<string> members = ad.GetMemberOf(de);

              for (int i = 0; i < members.Count; i++)
              {
            string delim = String.Empty;

            if (i < (members.Count - 1)) delim = ";";

            mb.Append(String.Format("{0}{1}", members[i].Trim(), delim));

            if (mb.Length > 0) info.memberOf = mb.ToString();
              }
              #endregion

              info.disabled = ad.IsDisabled(de).ToString();
              info.canonicalName = null;
              info.objectSID = new SecurityIdentifier(de.Properties["ObjectSid"].Value as byte[], 0).ToString();
              info.description = ad.GetStringProperty(de, "description");
              info.mDBUseDefaults = ad.GetStringProperty(de, "mDBUseDefaults");

              if (de.Properties.Contains("userAccountControl"))
            info.userAccountControl = de.Properties["userAccountControl"].Value.ToString();

              if (de.Properties.Contains("msExchMailboxGuid"))
            info.msExchMailboxGuid = new Guid(de.Properties["msExchMailboxGuid"].Value as byte[]).ToString();

              info.personalTitle = ad.GetStringProperty(de, "personalTitle");
              info.employeeType = ad.GetStringProperty(de, "employeeType");
              info.l = ad.GetStringProperty(de, "l");
              info.streetAddress = ad.GetStringProperty(de, "streetAddress");
              info.postalCode = ad.GetStringProperty(de, "postalCode");
              info.st = ad.GetStringProperty(de, "st");

              info.primarySMTP = ad.GetPrimarySMTP(de);

              infoList.Add(info);
              #endregion

              Console.Write("+");
            }
            else
              Console.Write("-");
              }
              catch { } // If there is a problem with an account, just move on silently.
        }
Example #2
0
        public void LastLogon(ADInfo info)
        {
            DateTime latestLogon = DateTime.Parse("12/31/1600 7:00:00 PM");

              info.canonicalName = null;
              info.lockoutTime = null;

              foreach (DomainController dc in DomainControllers)
              {
            try
            {
              using (DirectorySearcher ds = dc.GetDirectorySearcher())
              {
            ds.Filter = string.Format("(&(samAccountName={0}))", info.sAMAccountName);
            ds.PropertiesToLoad.Add("lastLogon");
            ds.PropertiesToLoad.Add("canonicalName");
            ds.PropertiesToLoad.Add("lockoutTime");
            ds.PropertiesToLoad.Add("pwdLastSet");
            ds.PropertiesToLoad.Add("proxyAddresses");
            ds.SizeLimit = 3;

            SearchResult sr = ds.FindOne();

            if (sr != null)
            {
              DateTime lastLogon = DateTime.Parse("12/31/1600 7:00:00 PM");

              if (sr.Properties.Contains("lastLogon"))
              {
                lastLogon = DateTime.FromFileTime((long)sr.Properties["lastLogon"][0]);
              }

              if (DateTime.Compare(lastLogon, latestLogon) > 0)
              {
                latestLogon = lastLogon;
              }

              if (info.canonicalName == null)
                info.canonicalName = sr.Properties["canonicalName"][0].ToString();

              if (info.lockoutTime == null)
                info.lockoutTime = sr.Properties["lockoutTime"][0].ToString();

              if (info.pwdLastSet == null)
                info.pwdLastSet = DateTime.FromFileTime((long)sr.Properties["pwdLastSet"][0]).ToString();

              //Console.WriteLine("{0} = {1}", dc.Name.StripCNFromName(), lastLogon);
            }
              }
            }
            catch { }
              }

              info.lastLogon = latestLogon.ToString();
        }
 partial void UpdateADInfo(ADInfo instance);
 partial void DeleteADInfo(ADInfo instance);
 partial void InsertADInfo(ADInfo instance);