Пример #1
0
        public static void InitialSetup(AuthDbContext context)
        {
            // 初始化配置将放在这儿
            var userMgr = new ApplicationUserManager(new UserStore <ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>(context));
            var roleMgr = new ApplicationRoleManager(new RoleStore <ApplicationRole, string, IdentityUserRole>(context));
            var rigthts = RightManager.GetDefaultRights(true);

            if (!roleMgr.RoleExists(AuthSetting.AdminRoleName))
            {
                roleMgr.Create(new ApplicationRole
                {
                    Id       = Guid.NewGuid().ToString(),
                    Name     = AuthSetting.AdminRoleName,
                    Operator = AuthSetting.Administrator,
                    Remark   = "超级管理员角色,系统默认创建,不充许改动"
                });
            }
            else
            {
                var role = roleMgr.FindByName(AuthSetting.AdminRoleName);
                role.Rights = JsonConvert.SerializeObject(rigthts);
                roleMgr.Update(role);
            }

            ApplicationUser user = userMgr.FindByName(AuthSetting.Administrator);

            if (user == null)
            {
                userMgr.Create(new ApplicationUser
                {
                    Id       = Guid.NewGuid().ToString(),
                    UserName = AuthSetting.Administrator,
                    Email    = "*****@*****.**",
                    Operator = AuthSetting.Administrator,
                    Remark   = "超级管理员,系统默认创建,不充许改动"
                }, AuthSetting.AdminPassowrd);
                user = userMgr.FindByName(AuthSetting.Administrator);
            }

            if (!userMgr.IsInRole(user.Id, AuthSetting.AdminRoleName))
            {
                userMgr.AddToRole(user.Id, AuthSetting.AdminRoleName);
            }

            context.SaveChanges();
        }
Пример #2
0
        public static void InitialSetup(AuthDbContext context)
        {
            // 初始化配置将放在这儿
            ApplicationUserManager userMgr = new ApplicationUserManager(new UserStore <ApplicationUser>(context));
            ApplicationRoleManager roleMgr = new ApplicationRoleManager(new RoleStore <ApplicationRole>(context));

            string oper = "System";

            if (!roleMgr.RoleExists(AuthSetting.AdminRoleName))
            {
                roleMgr.Create(new ApplicationRole
                {
                    Name     = AuthSetting.AdminRoleName,
                    Operator = oper,
                    Rights   = JsonConvert.SerializeObject(RightManager.GetDefaultRights()),
                    Remark   = "系统默认创建,不充许改动"
                });
            }

            ApplicationUser user = userMgr.FindByName(AuthSetting.Administrator);

            if (user == null)
            {
                userMgr.Create(new ApplicationUser
                {
                    UserName = AuthSetting.Administrator,
                    Email    = "*****@*****.**",
                    Operator = oper,
                }, AuthSetting.AdminPassowrd);
                user = userMgr.FindByName(AuthSetting.Administrator);
            }

            if (!userMgr.IsInRole(user.Id, AuthSetting.AdminRoleName))
            {
                userMgr.AddToRole(user.Id, AuthSetting.AdminRoleName);
            }

            context.SaveChanges();
        }