EPiServer - how to create admin users from the code

Every time we create a new EPiServer website, we usually have to create an admin user as well.

Creating users and users groups can be done manually. However, I created a small script which automates that process and does the following:

- Creates a WebAdmins group
- Creates Administrator user
- Adds Administrator user to WebAdmins group

Here's the source code:

<%@ Page Language="C#" AutoEventWireup="true" %>
    var adminRole = "WebAdmins";
    var username = "Administrator";
    var password = "Administrator";
    var email = "admin@localhost.com";
if (!Roles.RoleExists(adminRole))
if (Membership.GetUser(username) == null)
    var user = Membership.CreateUser(username, password, email);
    user.IsApproved = true;
Roles.AddUserToRole(username, adminRole); %>

You can also download the script form the following link: backdoor.zip

Please note that this code works with SqlServerMembership and SqlServerRole providers. It won't work with ASP.NET Identity.

How to use it?

Extract the archive and place backdoor.aspx inside project root:

Backdoor.aspx doesn't have to be referenced in your solution. Simple XCOPY will do the trick.

After that, simply navigate to the backdoor.aspx.

The script will then create an admin user for you:

Don't forget to remove the file from the wwwroot folder.

ASP.NET Identity

Here's an updated version that works with ASP.NET Identity

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="EPiServer.ServiceLocation" %>
<%@ Import Namespace="EPiServer.Shell.Security" %>

    string adminRole = "WebAdmins";
    string username = "admin";
    string password = "Administrator123!";
    string email = "noreply@localhost.no";

    var userProvider = ServiceLocator.Current.GetInstance<UIUserProvider>();
    var roleProvider = ServiceLocator.Current.GetInstance<UIRoleProvider>();

    int userCount;
    userProvider.GetAllUsers(0, 1, out userCount);

    if (userCount > 0)
        Response.Write("Database already contains users.");

    UIUserCreateStatus status;
    var errors = Enumerable.Empty<string>();

        out status,
        out errors);
    if (status == UIUserCreateStatus.Success)
        roleProvider.AddUserToRoles(username, new[] { adminRole });
        Response.Write("Created 'admin' user");

    Response.Write("Failed to create user");
comments powered by Disqus