Ejemplo n.º 1
0
        protected void Application_Start()
        {
            using (var cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                // CreateDatabase will fail on azure because I can't connect to tempdb;
                var builder       = new SqlConnectionStringBuilder(cn.ConnectionString);
                var createDbError = CreateDatabase(builder);
                InitLog4NetDb(cn);
            }

            var log = log4net.LogManager.GetLogger(GetType());

            log.InfoFormat("Calling Application_Start");

            log.Debug("Performing Area Registration");
            AreaRegistration.RegisterAllAreas();
            log.Debug("Performing Global Configuration");
            GlobalConfiguration.Configure(WebApiConfig.Register);
            log.Debug("Registering GLobal Filters");
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            log.Debug("Registering Routes");
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            log.Debug("Registering Bundles");
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            // Force creation of auth schema.
            using (var authDbCtx = AuthDbContext.Create())
            {
                authDbCtx.Database.Log = (dbLog => log.Debug(dbLog));
                log.Info("Initialization tests for Autorization 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");
                }
            }
            // Force creation of app schema.
            using (var context = new ApplicationDbContext())
            {
                // TODO: Perhaps rethink doing this.
                context.Database.Log = (dbLog => log.Debug(dbLog));
                log.Info("Initialization tests for Application Schema");
                //TODO: Could probably be sped up, but its O(n^2) where n = 4
                foreach (var newCCN in CreditCardNetwork.GetNetworks())
                {
                    if (!context.CreditCardNetworks.Any(ccn => ccn.Id == newCCN.Id))
                    {
                        context.CreditCardNetworks.Add(newCCN);
                    }
                }
                context.SaveChanges();
            }
            log.InfoFormat("Application_Start exiting");
        }