/// <summary>
 /// Create the Authentication <see cref="System.Data.Entity.DbContext"/> if it does not exist.
 /// </summary>
 /// <param name="log"></param>
 public static void CreateAuthContext(string nameOrConnectionString = "DefaultConnection")
 {
     using (var authDbCtx = AuthDbContext.Create(nameOrConnectionString))
     {
         InitDbContext(authDbCtx);
     }
 }
예제 #2
0
        public void Setup()
        {
            var connection = DbConnectionFactory.CreateTransient();

            AuthCtx = new AuthDbContext(connection);
            AppCtx  = new ApplicationDbContext(connection);
        }
        private static void InitDbContext(AuthDbContext authDbCtx)
        {
            authDbCtx.Database.Log = (dbLog => log.Debug(dbLog));
            log.Info("Initialization tests for Authorization Schema");
            if (!authDbCtx.Roles.Any())
            {
                log.Info("No roles found in database. Creating roles");
                var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(authDbCtx));
                roleManager.Create(new IdentityRole("DBAs"));               // Gives database internals access
                roleManager.Create(new IdentityRole("Credit Card Admins")); // Gives access to CC info
            }

            if (!authDbCtx.Users.Any())
            {
                log.Info("No users found in database. Creating users");
                var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(authDbCtx));
                userManager.Create(new ApplicationUser
                {
                    Id             = "Administrator",
                    Email          = "*****@*****.**",
                    UserName       = "******",
                    EmailConfirmed = true,
                    PasswordHash   = userManager.PasswordHasher.HashPassword("Alm0nds!"),
                });

                userManager.AddToRole("Administrator", "DBAs");

                userManager.Create(new ApplicationUser
                {
                    Email          = "*****@*****.**",
                    Id             = "CCAdmin",
                    UserName       = "******",
                    EmailConfirmed = true,
                    PasswordHash   = userManager.PasswordHasher.HashPassword("Appl3s")
                });
                userManager.AddToRole("CCAdmin", "Credit Card Admins");
            }

            authDbCtx.SaveChanges();
        }