HomeDev GuideRecipesAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

ASP.NET Identity

Configure ASP.NET Identity to manage users and roles for CMS 13 authentication and authorization.

ASP.NET Identity is a flexible authentication module for managing users and roles in CMS 13. Control who accesses the CMS editing and admin UIs, and integrate with custom user stores or third-party identity providers.

Add the EPiServer.CMS.UI.AspNetIdentity NuGet package and call AddCmsAspNetIdentity in the Startup class:

public void ConfigureServices(IServiceCollection services) {
  ...
  services.AddCmsAspNetIdentity<ApplicationUser>();
  ...
}

The EPiServer.CMS.UI.AspNetIdentity NuGet package implements the following services needed by the Optimizely UI: UIUserManager, UIRoleProvider, SecurityEntityProvider, IQueryableNotificationUsers, and SignInManager. Manage users, roles, and access rights from the admin view. EPiServer.Cms.Shell.UI includes login and logout views by default.

Custom user model

Define a custom user model to extend the default identity with additional properties. Two approaches are available:

  • Inherit from EPiServer.Cms.UI.AspNetIdentity.ApplicationUser:

    public class CustomUser : ApplicationUser
    {
    // Your custom properties
    }
  • Inherit from Microsoft.AspNetCore.Identity.IdentityUser and the EPiServer.Shell.Security.IUIUser interface:

    public class CustomUser: IdentityUser, IUIUser {
      public string Comment {
        get;
        set;
      }
      public bool IsApproved {
        get;
        set;
      }
      public bool IsLockedOut {
        get;
        set;
      }
    
      [Column(TypeName = "datetime2")]
      public DateTime CreationDate {
        get;
        set;
      }
    
      [Column(TypeName = "datetime2")]
      public DateTime ? LastLockoutDate {
        get;
        set;
      }
    
      [Column(TypeName = "datetime2")]
      public DateTime ? LastLoginDate {
        get;
        set;
      }
    
      public string PasswordQuestion {
        get;
      }
    
      public string ProviderName {
        get {
          return "MyProviderName";
        }
      }
    
      [NotMapped]
      public string Username {
        get {
          return base.UserName;
        }
        set {
          base.UserName = value;
        }
      }
    }

After defining a custom user model, configure it in the Startup class:

public void ConfigureServices(IServiceCollection services) {
  ...
  services.AddCmsAspNetIdentity<CustomUser>();
  ...
}

Migration

Entity Framework (EF) Core migrations version-control the identity database schema as a custom user model evolves.

By default, EPiServer.CMS.UI.AspNetIdentity provides a built-in ApplicationDbContext class to work with identity entities like users and roles. Configure the migration assembly when calling AddCmsAspNetIdentity():

services.AddCmsAspNetIdentity<ApplicationUser>(configureSqlServerOptions: o => o.MigrationsAssembly("Your.Assembly"));

Create a migration for ApplicationDbContext with EF Core Tools:

dotnet ef migrations add InitialCreate

See the EF Core Migration guide.