Tuesday, April 22, 2014

Sitecore Admin User Password Reset

Some times the admin password for Sitecore instance is lost or the user admin account is locked.
One way to reset the admin password is by executing SQL scripts.

Another easy way is by placing below C# code in an aspx page and browsing it using the sitecore instance domain:

string resetPassword = String.Empty;
string userName = string.Empty;
string newPassword = "newPassword";
bool status = false;
bool isLocked = false;

using (new Sitecore.SecurityModel.SecurityDisabler())
{
 userName = @"sitecore\admin";
 MembershipUser user = Membership.GetUser(userName, true);
 if (user != null)
 {
  resetPassword = user.ResetPassword();
                status = user.ChangePassword(resetPassword, newPassword);
                isLocked = user.IsLockedOut;
                if (isLocked)
                {
                    user.UnlockUser();
                }
 }
}

Response.Write(String.Format("Password updated {0} for user - {1}", (status ? "successfully" : "failed"), userName));

This code can be placed in an aspx file kept inside \Website\sitecore\admin\ location.
Once the aspx page is created with the above snippet, browsing that aspx page will reset the admin password to the desired password given in variable newPassword.

Sample aspx page can be found in GitHub.