Optimizely CMS: Create admin account
A quick utility controller for bootstrapping a local admin account in Optimizely CMS using ASP.NET Identity.
Copy this API controller into your Optimizely CMS web project and update the adminUsername, adminEmail, and adminPassword constants with your desired credentials. Once the site is running, navigate to /api/account/createadmin in your browser. The controller will check if a user with the given email already exists — if so, it resets the password, unlocks the account, and re-approves it. If the user doesn't exist, it creates a new one. Either way, it then iterates through all existing roles in the system and assigns them to the user, effectively granting full admin access across the CMS.
using System.Text.Json;
using System.Text.Json.Serialization;
using EPiServer.Cms.UI.AspNetIdentity;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
/// <summary>
/// Account controller
/// </summary>
/// <param name="userManager"></param>
/// <param name="roleManager"></param>
[ApiController]
[Route("api/account")]
[ResponseCache(CacheProfileName = "NoCache")]
public class AccountApiController(
UserManager<ApplicationUser> userManager,
ApplicationRoleProvider<ApplicationUser> roleManager)
: ControllerBase
{
/// <summary>
/// Create Admin account
/// Access using the path /api/account/createadmin
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
// ReSharper disable once StringLiteralTypo
[HttpGet("createadmin")]
public async Task<JsonResult> CreateAdmin()
{
// ReSharper disable once StringLiteralTypo
const string adminUsername = "___________________";
// ReSharper disable once StringLiteralTypo
const string adminEmail = "____________@____________";
const string adminPassword = "__________________________";
if (await userManager.FindByEmailAsync(adminEmail) is { } user)
{
user = await UpdateUser(user, adminPassword);
}
else
{
user = await CreateUser(
adminUsername,
adminEmail,
adminPassword);
}
if (user is null)
{
throw new Exception(
$"Failed to create admin user: {adminEmail}");
}
var existingRoles = roleManager.GetAllRolesAsync();
await foreach (var role in existingRoles)
{
Console.WriteLine(
"Add user {0} to role {1}",
adminUsername,
role.Name);
await roleManager.AddUserToRolesAsync(
adminUsername,
[role.Name]);
}
JsonSerializerOptions options = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = true
};
return new JsonResult("Success", options);
}
private async Task<ApplicationUser> UpdateUser(
ApplicationUser user,
string password)
{
user.IsApproved = true;
user.LockoutEnabled = false;
user.LockoutEnd = null;
user.IsLockedOut = false;
user.LastLockoutDate = null;
await userManager.UpdateAsync(user);
await userManager.RemovePasswordAsync(user);
await userManager.AddPasswordAsync(user, password);
return await userManager.FindByEmailAsync(user.Email!);
}
private async Task<ApplicationUser> CreateUser(
string username,
string email,
string password)
{
var user = new ApplicationUser
{
UserName = username,
Email = email,
IsApproved = true,
};
var result = await userManager.CreateAsync(
user,
password);
if (!result.Succeeded)
{
throw new Exception("CreateAdmin failed");
}
return await userManager.FindByEmailAsync(email);
}
}