Try our conversational search powered by Generative AI!

Henrik Fransas
Oct 27, 2015
  11974
(5 votes)

How to create a admin sql user through code

Sometimes you need to have a sql user in your development environment and this is easy to create by code on startup of the application.

In this example I added an if statement so that this is only running if the application are running in debug mode and this is because I do not want this to be created in test och production, only in dev environment.

Important, if you copy this, change the username and password to fit your needs, this is just an example with a generated password!!!

using System.Configuration.Provider;
using System.Web.Security;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Logging.Compatibility;

namespace Alloy46.Business
{
    [InitializableModule]
    public class CreateAdminUserAndRoles : IInitializableModule
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(CreateAdminUserAndRoles));

        public void Initialize(InitializationEngine context)
        {
#if DEBUG
            var mu = Membership.GetUser("EpiSQLAdmin");

            if (mu != null) return;

            try
            {
                Membership.CreateUser("EpiSQLAdmin", "6hEthU", "EpiSQLAdmin@site.com");

                try
                {
                    this.EnsureRoleExists("WebEditors");
                    this.EnsureRoleExists("WebAdmins");

                    Roles.AddUserToRoles("EpiSQLAdmin", 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);
            }
        }
    }
}

 

Oct 27, 2015

Comments

Aria Zanganeh
Aria Zanganeh Jan 19, 2017 11:46 AM

Great idea. I could in production this can become security breach! I think we just need  to  have this in DEBUG mode and RELEASE mode we should exclude:

#define DEBUG 

Henrik Fransas
Henrik Fransas Jan 20, 2017 08:49 AM

Aria

This means that is is only running in debug-mode, but yes you are hardcoding a password and if someone reverse your code they will see it, but for that you can instead define the name and password in web.config (I do not think you get that info).

Kristoffer Lindén
Kristoffer Lindén Dec 14, 2017 09:54 AM

Updated version for CMS 11 is found here.

https://world.episerver.com/blogs/kristoffer-linden/dates/2017/12/create-episerver-login-account-by-code/

/Kristoffer

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog