Beispiel #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            var addPasswordResult = await _userManager.AddPasswordAsync(user, Input.NewPassword);

            if (!addPasswordResult.Succeeded)
            {
                foreach (var error in addPasswordResult.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
                return(Page());
            }

            await _signInManager.RefreshSignInAsync(user);

            StatusMessage = "Your password has been set.";

            return(RedirectToPage());
        }
Beispiel #2
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            var email = await _userManager.GetEmailAsync(user);

            if (Input.Email != email)
            {
                var setEmailResult = await _userManager.SetEmailAsync(user, Input.Email);

                if (!setEmailResult.Succeeded)
                {
                    var userId = await _userManager.GetUserIdAsync(user);

                    throw new InvalidOperationException($"Unexpected error occurred setting email for user with ID '{userId}'.");
                }
            }



            var phoneNumber = await _userManager.GetPhoneNumberAsync(user);

            if (Input.PhoneNumber != phoneNumber)
            {
                var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber);

                if (!setPhoneResult.Succeeded)
                {
                    var userId = await _userManager.GetUserIdAsync(user);

                    throw new InvalidOperationException($"Unexpected error occurred setting phone number for user with ID '{userId}'.");
                }
            }

            await _signInManager.RefreshSignInAsync(user);

            StatusMessage = "Your profile has been updated";
            return(RedirectToPage());
        }
Beispiel #3
0
        public async Task <IActionResult> OnPostAsync()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            await _userManager.SetTwoFactorEnabledAsync(user, false);

            await _userManager.ResetAuthenticatorKeyAsync(user);

            _logger.LogInformation("User with ID '{UserId}' has reset their authentication app key.", user.Id);

            await _signInManager.RefreshSignInAsync(user);

            StatusMessage = "Your authenticator app key has been reset, you will need to configure your authenticator app using the new key.";

            return(RedirectToPage("./EnableAuthenticator"));
        }
        public async Task <IActionResult> OnPostRemoveLoginAsync(string loginProvider, string providerKey)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            var result = await _userManager.RemoveLoginAsync(user, loginProvider, providerKey);

            if (!result.Succeeded)
            {
                var userId = await _userManager.GetUserIdAsync(user);

                throw new InvalidOperationException($"Unexpected error occurred removing external login for user with ID '{userId}'.");
            }

            await _signInManager.RefreshSignInAsync(user);

            StatusMessage = "The external login was removed.";
            return(RedirectToPage());
        }
Beispiel #5
0
        public async Task <IActionResult> AddRole(AddUserRole model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.FindByNameAsync(model.UserName);

            if (user == null)
            {
                return(BadRequest());
            }

            var role = await _roleManager.FindByNameAsync(model.SelectedRole);

            if (role == null)
            {
                return(BadRequest());
            }

            var result = await _userManager.AddToRoleAsync(user, role.Name);

            if (result.Succeeded)
            {
                await _userManager.UpdateSecurityStampAsync(user);

                await _signInManager.RefreshSignInAsync(user);

                return(RedirectToRoute("GetUserRoles", new { userName = user.UserName }));
            }

            AddErrors(result);

            return(View(model));
        }
        public async Task <IActionResult> RolePermissions(RolePermission model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var role = await _roleManager.Roles
                       .Include(x => x.Claims)
                       .SingleOrDefaultAsync(x => x.Id == model.RoleId);

            if (role == null)
            {
                return(NotFound());
            }

            var selectedPermissions = model.Keys.ToList();

            var roleClaims = role.Claims
                             .Where(x => x.ClaimType == ConstantPolicies.DynamicPermission)
                             .Select(x => x.ClaimValue)
                             .ToList();


            // add new permissions
            var newPermissions = selectedPermissions.Except(roleClaims).ToList();

            foreach (var permission in newPermissions)
            {
                role.Claims.Add(new RoleClaim
                {
                    ClaimType  = ConstantPolicies.DynamicPermission,
                    ClaimValue = permission,
                    GivenOn    = DateTime.Now
                });
            }

            // remove deleted permissions
            var removedPermissions = roleClaims.Except(selectedPermissions).ToList();

            foreach (var permission in removedPermissions)
            {
                var roleClaim = role.Claims
                                .SingleOrDefault(x =>
                                                 x.ClaimType == ConstantPolicies.DynamicPermission &&
                                                 x.ClaimValue == permission);

                if (roleClaim != null)
                {
                    role.Claims.Remove(roleClaim);
                }
            }

            var result = await _roleManager.UpdateAsync(role);

            if (result.Succeeded)
            {
                var users = await _userManager.GetUsersInRoleAsync(role.Name);

                foreach (var user in users)
                {
                    await _userManager.UpdateSecurityStampAsync(user);

                    await _signInManager.RefreshSignInAsync(user);
                }


                return(RedirectToRoute("GetRolePermissions", new { roleName = role.Name }));
            }

            AddErrors(result);

            return(View(model));
        }