Expire the Account

The user password is made to expire by calling the ExpirePasswordNow method and in the example here, the AccountExpirationDate property is set to 10 days in the future to retire the account.

        internal static bool ExpireTheAccount(string user)
        {
            // Creating the PrincipalContext
            PrincipalContext principalContext = null;
            try
            {
                principalContext = new PrincipalContext(ContextType.Domain, "fabrikam", "DC=fabrikam,DC=com");
            }
            catch (Exception e)
            {
                MessageBox.Show("Failed to create PrincipalContext. Exception: " + e);
                Application.Exit();
            }

            // Expiring the user account
            bool bReturn = false;
            UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, user);

            if (userPrincipal != null)
            {
                userPrincipal.ExpirePasswordNow();
                userPrincipal.AccountExpirationDate = DateTime.Today.AddDays(10);
                try
                {
                    userPrincipal.Save();
                    bReturn = true;
                }
                catch (Exception e)
                {
                    MessageBox.Show("Exception when expiring the account. " + e);
                }
            }
                      
            return bReturn;
        }

See Also

Reference

System.DirectoryServices.AccountManagement

Concepts

About System.DirectoryServices.AccountManagement
Using System.DirectoryServices.AccountManagement

Send comments about this topic to Microsoft.

Copyright © 2008 by Microsoft Corporation. All rights reserved.