Esempio n. 1
0
        public ActionResult AddGroup(SecurityRoleEntity AdRole)
        {
            if (AdRole == null)
            {
                return(View("Error"));
            }

            if (ModelState.IsValid)
            {
                var ApplicationUserDb = ApplicationDbContext.Create();
                ApplicationUserDb.SecurityRoles.Add(AdRole);
                ApplicationUserDb.SaveChanges();

                return(RedirectToAction("ManageUsers"));
            }

            return(View());
        }
Esempio n. 2
0
        internal static void Initilize(ApplicationDbContext db)
        {
            //var userManager = HttpContext
            // .Current.GetOwinContext()
            // .GetUserManager<ApplicationUserManager>();

            //var roleManager = HttpContext.Current
            //  .GetOwinContext()
            //  .Get<ApplicationRoleManager>();

            var userManager = new ApplicationUserManager(new UserStore <ApplicationUser>(db));
            var roleManager = new ApplicationRoleManager(new RoleStore <IdentityRole>(db));

            const string roleAdmin     = "Admin";
            const string roleBroadcast = "BroadCastMessage";

            const string AdminName     = "*****@*****.**";
            const string AdminPassword = "******";

            const string BroadCastMessageName     = "*****@*****.**";
            const string BroadCastMessagePassword = "******";

            //Create Role Admin if it does not exist
            var role = roleManager.FindByName(roleAdmin);

            if (role == null)
            {
                role = new IdentityRole(roleAdmin);
                var roleresult = roleManager.Create(role);
            }

            //Create Role BroadCast if it does not exist
            role = roleManager.FindByName(roleBroadcast);
            if (role == null)
            {
                role = new IdentityRole(roleBroadcast);
                var roleresult = roleManager.Create(role);
            }

            //Create Default Users
            var user = userManager.FindByName(AdminName);

            if (user == null)
            {
                user = new ApplicationUser {
                    UserName = AdminName, Email = AdminName
                };
                var result = userManager.Create(user, AdminPassword);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            // Add user admin to Role Admin if not already added
            var rolesForUser = userManager.GetRoles(user.Id);

            if (!rolesForUser.Contains(roleAdmin))
            {
                var result = userManager.AddToRole(user.Id, roleAdmin);
            }

            user = userManager.FindByName(BroadCastMessageName);

            if (user == null)
            {
                user = new ApplicationUser {
                    UserName = BroadCastMessageName, Email = BroadCastMessageName
                };
                var result = userManager.Create(user, BroadCastMessagePassword);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            // Add user BroadCastMessage to Role BroadCastMessage if not already added
            rolesForUser = userManager.GetRoles(user.Id);
            if (!rolesForUser.Contains(roleBroadcast))
            {
                var result = userManager.AddToRole(user.Id, roleBroadcast);
            }

            SecurityRoleEntity sRole = new SecurityRoleEntity();

            sRole.Name         = "BUILTIN\\Users";
            sRole.IdentityRole = roleAdmin;

            db.SecurityRoles.Add(sRole);
        }
 /// <summary>
 /// 构造函数。
 /// </summary>
 public SecurityFactoryProvider()
 {
     this.securityRegsiterEntity = new SecurityRegsiterEntity();
     this.securityRoleEntity = new SecurityRoleEntity();
 }