Beispiel #1
0
        // In this method we will create default User roles and Admin user for login
        public void createRolesAndUsers()
        {
            var context     = TheAmazingRaceDbContext.Create();
            var roleManager = new RoleManager <Role>(new RoleStore <Role>(context));
            var userManager = new UserManager <User>(new UserStore <User>(context));


            // In Startup iam creating first Admin Role and creating a default Admin User
            if (!roleManager.RoleExists("Administrator"))
            {
                var roles = new List <Role>
                {
                    new Role {
                        Name = "Administrator"
                    },
                    new Role {
                        Name = "Staff"
                    },
                    new Role {
                        Name = "Participant"
                    }
                };

                roles.ForEach(r => roleManager.Create(r));

                ////Here we create a Admin super user who will maintain the website
                var admin = new User
                {
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    FirstName = "Super",
                    LastName  = "Admin",
                    DOB       = DateTime.Now,
                    Gender    = "Male",
                    CreatedBy = null,
                    CreatedOn = DateTime.Now
                };

                var result = userManager.Create(admin, "12345678");
                if (result.Succeeded)
                {
                    userManager.AddToRole(admin.Id, "Administrator");
                }
            }
        }
        public static RoleManager <IdentityRole> RoleManager()
        {
            TheAmazingRaceDbContext context = TheAmazingRaceDbContext.Create();

            return(new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)));
        }