示例#1
0
        public async Task <IActionResult> Index(DynamicAccessIndexViewModel viewModel)
        {
            var Result = await _roleManager.AddOrUpdateClaimsAsync(viewModel.RoleId, ConstantPolicies.DynamicPermissionClaimType, viewModel.ActionIds);

            if (!Result.Succeeded)
            {
                ModelState.AddModelError(string.Empty, "در حین انجام عملیات خطایی رخ داده  است.");
            }

            return(RedirectToAction("Index", new { id = viewModel.RoleId }));
        }
        public async Task <IdentityResult> SeedDatabaseWithAdminUserAsync()
        {
            var adminUserSeed = _adminUserSeedOptions.Value.AdminUserSeed;

            var name      = adminUserSeed.Username;
            var password  = adminUserSeed.Password;
            var email     = adminUserSeed.Email;
            var roleName  = adminUserSeed.RoleName;
            var firstName = adminUserSeed.FirstName;
            var lastName  = adminUserSeed.LastName;

            var thisMethodName = nameof(SeedDatabaseWithAdminUserAsync);

            var adminUser = await _applicationUserManager.FindByNameAsync(name);

            if (adminUser != null)
            {
                _logger.LogInformation($"{thisMethodName}: adminUser already exists.");
                return(IdentityResult.Success);
            }

            //Create the `Admin` Role if it does not exist
            var adminRole = await _roleManager.FindByNameAsync(roleName);

            if (adminRole == null)
            {
                adminRole = new AppRole(roleName);
                var adminRoleResult = await _roleManager.CreateAsync(adminRole);

                if (adminRoleResult == IdentityResult.Failed())
                {
                    _logger.LogError($"{thisMethodName}: adminRole CreateAsync failed. {adminRoleResult.DumpErrors()}");
                    return(IdentityResult.Failed());
                }
            }
            else
            {
                _logger.LogInformation($"{thisMethodName}: adminRole already exists.");
            }



            adminUser = new AppUser
            {
                UserName         = name,
                Email            = email,
                EmailConfirmed   = true,
                LockoutEnabled   = true,
                RegisterDateTime = DateTime.Now,
                FirstName        = firstName,
                LastName         = lastName,
                //Gender=null,
                IsActive = true
            };
            var adminUserResult = await _applicationUserManager.CreateAsync(adminUser, password);

            if (adminUserResult == IdentityResult.Failed())
            {
                _logger.LogError($"{thisMethodName}: adminUser CreateAsync failed. {adminUserResult.DumpErrors()}");
                return(IdentityResult.Failed());
            }

            var setLockoutResult = await _applicationUserManager.SetLockoutEnabledAsync(adminUser, enabled : false);

            if (setLockoutResult == IdentityResult.Failed())
            {
                _logger.LogError($"{thisMethodName}: adminUser SetLockoutEnabledAsync failed.");
                return(IdentityResult.Failed());
            }

            var addToRoleResult = await _applicationUserManager.AddToRoleAsync(adminUser, adminRole.Name);

            if (addToRoleResult == IdentityResult.Failed())
            {
                _logger.LogError($"{thisMethodName}: adminUser AddToRoleAsync failed. {addToRoleResult.DumpErrors()}");
                return(IdentityResult.Failed());
            }

            ICollection <ControllerViewModel> securedControllerActions = _mvcActionsDiscovery.GetAllSecuredControllerActionsWithPolicy(ConstantPolicies.DynamicPermission);
            IList <string> allSecuredActions = securedControllerActions.SelectMany(s => s.MvcActions).ToList().Select(a => a.ActionId).ToList();

            await _roleManager.AddOrUpdateClaimsAsync(adminRole.Id, ConstantPolicies.DynamicPermissionClaimType, allSecuredActions);

            return(IdentityResult.Success);
        }