Check Password Expiration Date of Active Directory using C#


  public void checkifPasswordExpired(string domainname, string userName, string Password)
        {
            try
            {
                object o = GetPasswordExpirationDate(userName, domainname);
                DateTime expDate = DateTime.Parse(o.ToString());
                DateTime curr = System.DateTime.Now;
                if (curr > expDate)
                {
                    Response.Redirect("PasswordChange.aspx?FormLogin=yes");
                }
            }
            catch (Exception ex)
            {

            }
        }



  public object GetPasswordExpirationDate(string userId, string domainOrMachineName)
        {

            try
            {
                // code that executes under the new context...
                string ldapPath = LDapUrl;
                DirectoryEntry directionEntry = new DirectoryEntry(ldapPath, "onactuate" + "\\" + txtUsername.Text, txtCurrentPassword.Text);
                using (var userEntry = new DirectoryEntry("WinNT://" + domainOrMachineName + '/' + userId + ",user"))
               
                {

                    return userEntry.InvokeGet("PasswordExpirationDate");
                }
            }

            catch (Exception ex)
            {
               

            }

        }

No comments:

Post a Comment