Change Active Directory password using C#

 protected void Button1_Click(object sender, EventArgs e)
        {

            string usernamewithoutdomain;
            usernamewithoutdomain = txtUsername.Text;

            if (txtUsername.Text.Contains(@"\"))// split string
            {
                usernamewithoutdomain = txtUsername.Text.Substring(txtUsername.Text.LastIndexOf('\\') + 1);

            }

            ChangeMyPassword("domainname", usernamewithoutdomain, txtCurrentPassword.Text, txtNewPassword.Text);

        }
        public void ChangeMyPassword(string domainName, string userName, string currentPassword, string newPassword)
        {
            try
            {
                string ldapPath = LDapUrl;// directory path
                DirectoryEntry directionEntry = new DirectoryEntry(ldapPath, domainName + "\\" + userName, currentPassword);
                if (directionEntry != null)
                {
                    DirectorySearcher search = new DirectorySearcher(directionEntry);
                    search.Filter = "(SAMAccountName=" + userName + ")";
                    using (var context = new PrincipalContext(ContextType.Domain))
                    {
                        using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName))
                        {
                            user.ChangePassword(currentPassword, newPassword);

                        }
                    }

                }
            }

            catch (Exception ex)
            { 
            }
            finally
            {
              
            }

        }

    }

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)
            {
               

            }

        }

Force user to change password on first login in Active Directory using C#


If user Login first time then force user to change password on first Login

 public bool FirstTimeUser(string userName, string Password, string domain)
        {

            try
            {
                string ldapPath = LDapUrl;
                DirectoryEntry directionEntry = new DirectoryEntry(ldapPath, domain + "\\" + userName, Password);
                if (directionEntry != null)
                {
                    DirectorySearcher search = new DirectorySearcher(directionEntry);

                    search.Filter = "(SAMAccountName=" + userName + ")";
                    using (var pctx = new PrincipalContext(ContextType.Domain))
                    {

                        using (var p = UserPrincipal.FindByIdentity(pctx, IdentityType.SamAccountName, userName))
                        {
                            if (p == null)
                                return false;
                            bool ISPasswordCorrect = pctx.ValidateCredentials(userName, Password, ContextOptions.Negotiate);

                            if (p.LastPasswordSet.HasValue == false && p.PasswordNeverExpires == false)
                            {                              
                                Session["UserName"] = userName; // pass user name in session and get it on another page
                                Response.Redirect("Default.aspx?ChangePasswordOnFirstLogin=yes");
                            }
                            else if (ISPasswordCorrect == false)
                            {
                               lblErrormsg.Visible = true;
                               lblErrormsg.Text = "The username or password is incorrect";
                            }

                        }
                    }

                }

            }


            catch (MultipleMatchesException mmex)
            {
                string errorMsg;
                errorMsg = "TestAdShouldChangePassword( ad user = '" + userName + "' ) - Exception finding user, can't determine if ad says to change password, returing false : Ex = " + mmex.Message;

            }

            return false;
        }

Check Active directory user using C#


Check user existence in Active Directory


        public bool DoesUserExist(string userName, string domain)
        {
            try
            {
                using (var domainContext = new PrincipalContext(ContextType.Domain, domain))
                {
                    using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, userName))
                    {
                        return foundUser != null;
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
     

Validate user against Active Directory


 public void  UserExists(string domainname, string userName, string Password)
        {
            try
            {
                string ldapPath = LDapUrl;// path of directory
                DirectoryEntry directionEntry = new DirectoryEntry(ldapPath, domainname + "\\" + userName, Password);
                if (directionEntry != null)
                {
                    DirectorySearcher search = new DirectorySearcher(directionEntry);
                    search.Filter = "(SAMAccountName=" + userName + ")";
                    SearchResult result = search.FindOne();
                    if (result != null)
                    {
                     
                        Response.Redirect(http;//www.google.com, false)
                    }

                }
            }
            catch (DirectoryServicesCOMException ex)
            {

              ex.Message;
            }

        }