Exemplo n.º 1
0
        public static async void InitializeAsync(GTKeeperContext context) //SchoolContext is EF context
        {
            context.Database.EnsureCreated();                             //if db is not exist ,it will create database .but ,do nothing .

            // Look for any students.
            if (context.Users.Any())
            {
                return;   // DB has been seeded
            }

            var user =
                new GTKeeperUser
            {
                UserName           = "******",
                NormalizedUserName = "******",
                Email           = "*****@*****.**",
                NormalizedEmail = "*****@*****.**",
                EmailConfirmed  = true,
                LockoutEnabled  = false,
                SecurityStamp   = Guid.NewGuid().ToString(),
                FirstName       = "Gabriel",
                LastName        = "Tamé"
            };


            var roleStore = new RoleStore <IdentityRole>(context);

            if (!context.Roles.Any(r => r.Name == "admin"))
            {
                await roleStore.CreateAsync(new IdentityRole { Name = "admin", NormalizedName = "admin" });
            }

            if (!context.Users.Any(u => u.UserName == user.UserName))
            {
                var password = new PasswordHasher <GTKeeperUser>();
                var hashed   = password.HashPassword(user, "password");
                user.PasswordHash = hashed;
                var userStore = new UserStore <GTKeeperUser>(context);
                await userStore.CreateAsync(user);

                await userStore.AddToRoleAsync(user, "admin");
            }

            await context.SaveChangesAsync();
        }
Exemplo n.º 2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, GTKeeperAPI.Models.GTKeeperContext context, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();


            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                // Shows UseCors with named policy.
                app.UseCors("AllowAllHeaders");
            }

            //Inicializamos la bd
            DbInitializer.InitializeAsync(context);



            app.UseDefaultFiles();
            app.UseStaticFiles();
            app.UseAuthentication();
            app.UseMvc();
        }