Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Pankaj 11 posts 32 karma points
    Jun 15, 2024 @ 10:54
    Pankaj
    1

    How to create umbraco BackOffice admin user using code c#

    How to create umbraco BackOffice admin user using code c#

  • Matty 34 posts 148 karma points
    Jun 16, 2024 @ 09:18
    Matty
    0

    Assuming you have a SurfaceController or UmbracoController or something then I'd be injecting an IBackOfficeUserManager and using that.

    Something like this should get you started:

    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Options;
    using Umbraco.Cms.Core.Configuration.Models;
    using Umbraco.Cms.Core.Security;
    
    // change this namespace to your own
    namespace Umbraco.Cms.Web.UI.Controllers;
    
    public class SomethingController : UmbracoController
    {
        private readonly IBackOfficeUserManager _backOfficeUserManager;
        private readonly GlobalSettings _globalSettings;
    
        public SomethingController(
            IBackOfficeUserManager backOfficeUserManager,
            IOptions<GlobalSettings> globalSettings)
        {
            _backOfficeUserManager = backOfficeUserManager;
            _globalSettings = globalSettings.Value;
        }
    
        public async Task<IActionResult> Index(string name, string email, string password)
        {
            var identityUser = BackOfficeIdentityUser.CreateNew(
                _globalSettings,
                email,
                email,
                _globalSettings.DefaultUILanguage);
    
            identityUser.Name = name;
    
            IdentityResult created = await _backOfficeUserManager.CreateAsync(identityUser, password);
    
            return created.Succeeded ? Ok() : BadRequest();
        }
    }
    

    That should at least get you started

Please Sign in or register to post replies

Write your reply to:

Draft