Skip to content

Instantly share code, notes, and snippets.

@davidisnotnull
Last active July 2, 2020 11:20
Show Gist options
  • Save davidisnotnull/58611662abce98aadb47e5f8173bd0b3 to your computer and use it in GitHub Desktop.
Save davidisnotnull/58611662abce98aadb47e5f8173bd0b3 to your computer and use it in GitHub Desktop.
Creates an Episerver Admin user with appropriate roles on a local db.
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Logging.Compatibility;
using System.Configuration.Provider;
using System.Web.Security;
namespace MyEpiserverProject.Business.Initialization
{
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class CreateAdminUserAndRoles : IInitializableModule
{
private static readonly ILog Log = LogManager.GetLogger(typeof(CreateAdminUserAndRoles));
public void Initialize(InitializationEngine context)
{
#if DEBUG
var mu = Membership.GetUser("EpiLocalAdmin");
if (mu != null) return;
try
{
Membership.CreateUser("EpiLocalAdmin", "P@ssw0rd!", "[email protected]");
try
{
this.EnsureRoleExists("WebEditors");
this.EnsureRoleExists("WebAdmins");
Roles.AddUserToRoles("EpiLocalAdmin", new[] { "WebAdmins", "WebEditors" });
}
catch (ProviderException pe)
{
Log.Error(pe);
}
}
catch (MembershipCreateUserException mcue)
{
Log.Error(mcue);
}
#endif
}
public void Uninitialize(InitializationEngine context)
{
}
private void EnsureRoleExists(string roleName)
{
if (Roles.RoleExists(roleName)) return;
try
{
Roles.CreateRole(roleName);
}
catch (ProviderException pe)
{
Log.Error(pe);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment