How to reset admin or any user password in Umbraco?

566 views Asked by At
1

There are 1 answers

0
Oltion Driza On

Create a class as below and run the project:

using System.Web.Security;
using Umbraco.Core.Composing;
using Umbraco.Core;
using Umbraco.Core.Services;
using Umbraco.Web.Security.Providers;

namespace Project
{
    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class StartingComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Components().Append<StartingEvent>();
        }
    }
    public class StartingEvent : IComponent
    {
        private readonly IUserService _userService; 
        public StartingEvent(IUserService userService)
        {
            _userService = userService;
            var adminUser = _userService.GetUserById(-1);
            adminUser.Username = adminUser.Email = "[email protected]";
            adminUser.FailedPasswordAttempts = 0;
            adminUser.IsLockedOut = false;
            adminUser.IsApproved = true;
            adminUser.RawPasswordValue = (Membership.Providers["UsersMembershipProvider"] as UsersMembershipProvider)?.HashPasswordForStorage("Admin123*");
            userService.Save(adminUser);
        }
        public void Initialize()
        {
        }
        public void Terminate()
        {
        }
    }
}