SaaS CMS has officially launched! Learn more now.

ASP.NET Identity provider in the CMS login


Hello readers,

I'm working on implementing ASP.NET Identity in an EPiServer project, following this tutorial:

In order to create an initial user I made an 'InitializeModule', like this:

public class MyInitializeModule : EPiServer.ServiceLocation.IConfigurableModule
    private readonly string[] _roles = new string[] { "WebAdmins", "WebEditors" };

    public void ConfigureContainer(EPiServer.ServiceLocation.ServiceConfigurationContext context)
        //Configure your providers

    public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context)
        //Add initialization logic, this method is called once after CMS has been initialized
        using (UserStore store = new UserStore(new ApplicationDbContext("EPiServerDB")))
            //If there's already a user, then we don't need a seed
            if (store.Users.Any())

            //We know that this Password hasher is used as it's configured
            IPasswordHasher hasher = new PasswordHasher();
            string passwordHash = hasher.HashPassword( /* obfuscated */);

            ApplicationUser administrator = new ApplicationUser();
            administrator.Email = "";
            administrator.EmailConfirmed = true;
            administrator.LockoutEnabled = true;
            administrator.UserName = "Admin";
            administrator.PasswordHash = passwordHash;
            administrator.SecurityStamp = Guid.NewGuid().ToString();


            //Get the user associated with our username
            ApplicationUser createdUser = store.FindByNameAsync("Admin").GetAwaiter().GetResult();

            IUserRoleStore userRoleStore = store as IUserRoleStore;
            using (var roleStore = new RoleStore(new ApplicationDbContext("EPiServerDB")))
                IList userRoles = userRoleStore.GetRolesAsync(createdUser).GetAwaiter().GetResult();

                foreach (string roleName in this._roles)
                    if (roleStore.FindByNameAsync(roleName).GetAwaiter().GetResult() == null)
                        roleStore.CreateAsync(new IdentityRole { Name = roleName }).GetAwaiter().GetResult(); ;

                    if (!userRoles.Contains(roleName))
                        userRoleStore.AddToRoleAsync(createdUser, roleName).GetAwaiter().GetResult();


    public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context)

I can validate that the user (and roles) get added by looking at the database content.

When I try to login at site-url/episerver and use the credentials for the admin user, it won't let me login.

I have triple-checked the actions described in the blogpost, but I'm sure I've followed the instructions, yet it doesn't work.

Does anyone know how to fix my issue?



Mar 30, 2017 15:05

Hi Martin

Have you tried creating a clean Alloy site using the Episerver Visual Studio extension? This uses Identity by default (and has a controller that sets a new user up if none are configured). 

It may be worth comparing the code against that in the post to see if anything is missing?


Mar 30, 2017 16:54

Hi Martijn,

Do you need an initialization module or one-time thing?

Perhaps you could create an aspx page (userregistration.aspx) and drop it to the web root:

<%@ 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 = "";

    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>();

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

    Response.Write("Failed to create user");

When you navigate to, it will check if the database already contains users.

If not, it will create WebAdmins group and a user (username: admin, password: Administrator123!) that is a member of that group.

This aspx file doesn't even have to be included in the project (.csproj) and can be deleted right after the user is created.

Mar 30, 2017 19:56
<p>I've discarded the InitializationModule solution and went with the same as the alloy one.</p> <p>Thanks for the help! I managed to get it working after updating all the packages to the latest version.</p>
Mar 31, 2017 15:41

Seems like  administrator.IsApproved is set to false as default. I set the property to true and was able to login with this example. 

Edited, May 03, 2017 15:01
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.