示例#1
0
        public void CreateDefaultRolesAndUsers()
        {
            var context = new ApplicationDbContext();

            var roleManager = new CustomRoleStore(context);
            var userManager = new ApplicationUserManager(new CustomUserStore(context));


            if (!roleManager.RoleExists(UserRoles.ADMIN))
            {
                var role = new CustomRole()
                {
                    Name = UserRoles.ADMIN
                };
                roleManager.Create(role);

                var userAdmin = new ApplicationUser {
                    Name = "Administrador", UserName = "******", Email = "*****@*****.**"
                };
                var userPWD = "GoatStar_2020";
                var result  = userManager.Create(userAdmin, userPWD);

                if (result.Succeeded)
                {
                    var res = userManager.AddToRole(userAdmin.Id, UserRoles.ADMIN);
                }
            }

            if (!roleManager.RoleExists(UserRoles.SUPERVISOR))
            {
                var role = new CustomRole {
                    Name = UserRoles.SUPERVISOR
                };
                roleManager.Create(role);
            }

            if (!roleManager.RoleExists(UserRoles.OPERATOR))
            {
                var role = new CustomRole {
                    Name = UserRoles.OPERATOR
                };
                roleManager.Create(role);
            }
        }