Exemplo n.º 1
0
        internal static void Initialize(AppDbContext context, Microsoft.AspNetCore.Identity.RoleManager <Microsoft.AspNetCore.Identity.IdentityRole> roleManager, Microsoft.AspNetCore.Identity.UserManager <Microsoft.AspNetCore.Identity.IdentityUser> userManager)
        {
            if (!roleManager.RoleExistsAsync("Admin").Result)
            {
                IdentityRole role = new IdentityRole("Admin");

                roleManager.CreateAsync(role).Wait();
            }

            if (!roleManager.RoleExistsAsync("NormalUser").Result)
            {
                IdentityRole role = new IdentityRole("NormalUser");

                roleManager.CreateAsync(role).Wait();
            }

            //-------------------------------------------------------------------

            if (userManager.FindByNameAsync("Guru").Result == null)
            {
                IdentityUser user = new IdentityUser();
                user.Email    = "*****@*****.**";
                user.UserName = "******";

                IdentityResult result = userManager.CreateAsync(user, "Password!123").Result;

                if (result.Succeeded)
                {
                    userManager.AddToRoleAsync(user, "Admin").Wait();
                }
            }

            if (userManager.FindByNameAsync("Sven").Result == null)
            {
                IdentityUser user = new IdentityUser();
                user.Email    = "*****@*****.**";
                user.UserName = "******";

                IdentityResult result = userManager.CreateAsync(user, "Password!123").Result;

                if (result.Succeeded)
                {
                    userManager.AddToRoleAsync(user, "NormalUser").Wait();
                }
            }

            //-----------------------------------------------------------------------

            // Don´t forget to save
            //context.SaveChanges();
        }
Exemplo n.º 2
0
        internal static void Initialize(DbContext context, Microsoft.AspNetCore.Identity.RoleManager <Microsoft.AspNetCore.Identity.IdentityRole> roleManager, Microsoft.AspNetCore.Identity.UserManager <Microsoft.AspNetCore.Identity.IdentityUser> userManager)
        {
            if (!roleManager.RoleExistsAsync("Admin").Result)
            {
                IdentityRole role = new IdentityRole("Admin");

                roleManager.CreateAsync(role).Wait();
            }

            if (!roleManager.RoleExistsAsync("NormalUser").Result)
            {
                IdentityRole role = new IdentityRole("NormalUser");

                roleManager.CreateAsync(role).Wait();
            }

            //------------------------------------------------------------------------------------//

            if (userManager.FindByNameAsync("Abduallah").Result == null)
            {
                IdentityUser user = new IdentityUser();
                user.Email    = "*****@*****.**";
                user.UserName = "******";

                IdentityResult result = userManager.CreateAsync(user, "Abduallah1996-").Result;

                if (result.Succeeded)
                {
                    userManager.AddToRoleAsync(user, "Admin").Wait();
                }
            }

            if (userManager.FindByNameAsync("Abdullah").Result == null)
            {
                IdentityUser user = new IdentityUser();
                user.Email    = "*****@*****.**";
                user.UserName = "******";


                var result = userManager.CreateAsync(user, "Abduallah1996-");

                if (result.IsCompletedSuccessfully)
                {
                    userManager.AddToRoleAsync(user, "NormalUser").Wait();
                }
            }
            //------------------------------------------------------------------------------------------

            //Glöm inte att spara
            //context.SaveChanges();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create the default application roles
        /// </summary>
        /// <param name="userMgr"></param>
        /// <param name="roleMgr"></param>
        /// <returns></returns>
        public async Task <bool> EnsureSeedData(Microsoft.AspNetCore.Identity.UserManager <ApplicationUser> userMgr, Microsoft.AspNetCore.Identity.RoleManager <IdentityRole> roleMgr)
        {
            var adminRole = await roleMgr.FindByNameAsync("Admin");

            if (adminRole == null)
            {
                adminRole = new IdentityRole("Admin");
                await roleMgr.CreateAsync(adminRole);
            }

            var userRole = await roleMgr.FindByNameAsync("User");

            if (userRole == null)
            {
                userRole = new IdentityRole("User");
                await roleMgr.CreateAsync(userRole);
            }
            return(true);
        }
Exemplo n.º 4
0
        public static void SeedData(Microsoft.AspNetCore.Identity.RoleManager <IdentityRole> roleManager)
        {
            if (!roleManager.RoleExistsAsync(RoleConstants.Administrator).Result)
            {
                var adminRole = new IdentityRole()
                {
                    Name = RoleConstants.Administrator
                };
                roleManager.CreateAsync(adminRole).Wait();
            }

            if (!roleManager.RoleExistsAsync(RoleConstants.Visitor).Result)
            {
                var visitorRole = new IdentityRole()
                {
                    Name = RoleConstants.Visitor
                };
                roleManager.CreateAsync(visitorRole).Wait();
            }
        }