How To Reset Main Umbraco 7 Admin Password Programatically If Locked Out

It happens to the best of us! Sometimes you lock yourself out of your home, car and sometimes even Umbraco! Maybe you've inherited an Umbraco website from someone else and the client does not have the login details for the main acccount (i.e. user Id = 0). Or perhaps you've logged in a few times incorrectly and now the account is locked!

The main Umbraco account is special, as by default it will not be visible in the Users section of Umbraco to any other users - including other admins. So being able to log in as another admin, doesn't allow you to reset the main admins login details or unlock their account.

You can go into the database and manually reset the password, but this is made slightly more complicated now given that some versions of Umbraco 7 use slightly different hashes to others. While there is now a password reset facility, typically you would need to either have valid SMTP login details or a copy of something like SMTP4DEV to allow you to 'capture' the password reset email if this is a development website.

Well here is a neat solution we find useful which sidesteps all of these! You can programatically reset the main admins password - and this will work on any version of Umbraco 7.¬† Simply copy the below code into a .cs file in ~/App_Code and this will unlock, validate and reset both the email address (to "[email protected]") and password (to "abc123abc123"). Once saved, simply restart IIS to trigger the event that will run this code! Once IIS has started, you can simply delete this file (as you don't want it re-setting the main admin each time the site starts!)

using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using System;
using System.IO;
using System.Text;
using System.Configuration;
using umbraco.IO;

namespace FarmCode.PasswordReset
     public class RegisterEvents : ApplicationEventHandler
          protected override void ApplicationStarted(UmbracoApplicationBase 
                    umbracoApplication, ApplicationContext applicationContext)
               UmbracoApplicationBase.ApplicationInit += ResetAdminPassword;

          public void ResetAdminPassword(object sender, EventArgs e)
               var userService = ApplicationContext.Current.Services.UserService;

               var adminUser = userService.GetUserById(0);
               adminUser.Username = adminUser.Email = "[email protected]";
               adminUser.FailedPasswordAttempts = 0;
               adminUser.IsLockedOut = false;
               adminUser.IsApproved = true;
               userService.SavePassword(adminUser, "abc123abc123");

Need an Umbraco Master?

Here at Simon Antony, we have an in house certified Umbraco Grand Master available for hire. Got a problem with your site, need architecture advice, give us a call to speak to Simon directly and see how we can help

Contact Simon Today!