Ejemplo n.º 1
0
        private void InitializeIdentity(ApplicationDbContext context)
        {
            #region Contracts

            if (context == null)
            {
                throw new ArgumentNullException();
            }

            #endregion

            // Default - User
            const string adminUserName     = "******";
            const string adminUserPassword = "******";

            // Default - Role
            const string adminRoleName = "Admin";

            // Default - Permission
            const string aboutPermissionName   = "About";
            const string contactPermissionName = "Contact";


            // Manager
            var userManager       = HttpContext.Current.GetOwinContext().Get <ApplicationUserManager>();
            var roleManager       = HttpContext.Current.GetOwinContext().Get <ApplicationRoleManager>();
            var permissionManager = HttpContext.Current.GetOwinContext().Get <ApplicationPermissionManager>();

            // User
            var user = userManager.FindByName(adminUserName);
            if (user == null)
            {
                user = new ApplicationUser {
                    UserName = adminUserName, Email = adminUserName
                };
                userManager.Create(user, adminUserPassword);
                userManager.SetLockoutEnabled(user.Id, false);
            }

            // Role
            var role = roleManager.FindByName(adminRoleName);
            if (role == null)
            {
                role = new ApplicationRole(adminRoleName);
                roleManager.Create(role);
            }

            // Permission
            var aboutPermission = permissionManager.FindByName(aboutPermissionName);
            if (aboutPermission == null)
            {
                aboutPermission = new ApplicationPermission(aboutPermissionName);
                permissionManager.Create(aboutPermission);
            }

            var contactPermission = permissionManager.FindByName(contactPermissionName);
            if (contactPermission == null)
            {
                contactPermission = new ApplicationPermission(contactPermissionName);
                permissionManager.Create(contactPermission);
            }


            // UserAddToRole
            var rolesForUser = userManager.GetRoles(user.Id);
            if (rolesForUser.Contains(role.Name) == false)
            {
                userManager.AddToRole(user.Id, role.Name);
            }

            // PermissionAddToRole
            var rolesForAboutPermission = permissionManager.GetRolesById(aboutPermission.Id);
            if (rolesForAboutPermission.Contains(role.Name) == false)
            {
                permissionManager.AddToRole(aboutPermission.Id, role.Name);
            }

            var rolesForContactPermission = permissionManager.GetRolesById(contactPermission.Id);
            if (rolesForContactPermission.Contains(role.Name) == false)
            {
                permissionManager.AddToRole(contactPermission.Id, role.Name);
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            using (var datebaseContext = CreateDatabaseContext <ApplicationDbContext>())
            {
                // Const
                const string userName       = "******";
                const string password       = "******";
                const string email          = "*****@*****.**";
                const string roleName       = "admin";
                const string permissionName = "write";

                // Manager
                var userManager       = new ApplicationUserManager(datebaseContext);
                var roleManager       = new ApplicationRoleManager(datebaseContext);
                var permissionManager = new ApplicationPermissionManager(datebaseContext);

                // User
                var user = userManager.FindByName(userName);
                if (user == null)
                {
                    user = new ApplicationUser {
                        UserName = userName, Email = email
                    };
                    userManager.Create(user, password);
                    userManager.SetLockoutEnabled(user.Id, false);
                }

                // Role
                var role = roleManager.FindByName(roleName);
                if (role == null)
                {
                    role = new ApplicationRole(roleName);
                    roleManager.Create(role);
                }

                // Permission
                var permission = permissionManager.FindByName(permissionName);
                if (permission == null)
                {
                    permission = new ApplicationPermission(permissionName);
                    permissionManager.Create(permission);
                }

                // UserRole
                var userRoleNames = userManager.GetRoles(user.Id);
                if (userRoleNames.Contains(role.Name) == false)
                {
                    userManager.AddToRole(user.Id, role.Name);
                }

                // PermissionRole
                var permissionRoleNames = permissionManager.GetRolesById(permission.Id);
                if (permissionRoleNames.Contains(role.Name) == false)
                {
                    permissionManager.AddToRole(permission.Id, role.Name);
                }
            }

            Console.Write("End...");
            Console.ReadLine();
        }