Beispiel #1
0
 // Constructors
 public ApplicationPermissionAuthorize(ApplicationPermissionManager permissionManager) : base(permissionManager)
 {
 }
Beispiel #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();
        }