/// <summary>
 /// Throws an exception, if the specified <paramref name="identityResult"/> represents a failed operation result.
 /// </summary>
 /// <exception cref="IIdentityResultExtensions"></exception>
 public static void ThrowIfFailed(this IIdentityResult identityResult)
 {
     if (!identityResult.Succeeded)
     {
         throw new IdentityException(identityResult.Errors);
     }
 }
Пример #2
0
            public async Task <string> Handle(CreateRoleCommand request, CancellationToken cancellationToken)
            {
                ApplicationRole role = ConvertToApplicationRole(request);

                IIdentityResult identityResult = await _roleStorage
                                                 .CreateAsync(role)
                                                 .ConfigureAwait(false);

                identityResult.ThrowIfFailed();

                return(role.Id);
            }
Пример #3
0
            public async Task <string> Handle(CreateUserCommand request, CancellationToken cancellationToken)
            {
                ApplicationUser user = ConvertToApplicationUser(request);

                IIdentityResult identityResult = await _userPasswordService
                                                 .CreateUserWithPasswordAsync(user, request.Password)
                                                 .ConfigureAwait(false);

                identityResult.ThrowIfFailed();

                return(user.Id);
            }
Пример #4
0
            public async Task <Unit> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
            {
                ApplicationUser userToDelete = await _userStorage.FindByIdAsync(request.UserId)
                                               .ConfigureAwait(false)
                                               ?? throw new NotFoundException(nameof(ApplicationUser), request.UserId);

                IIdentityResult identityResult = await _userStorage.DeleteAsync(userToDelete)
                                                 .ConfigureAwait(false);

                identityResult.ThrowIfFailed();

                return(Unit.Value);
            }
            public async Task <Unit> Handle(ChangeUserPasswordCommand request, CancellationToken cancellationToken)
            {
                ApplicationUser user = await _userStorage.FindByIdAsync(request.UserId)
                                       .ConfigureAwait(false)
                                       ?? throw new NotFoundException(nameof(ApplicationUser), request.UserId);

                IIdentityResult identityResult = await _userPasswordService
                                                 .ChangeUserPasswordAsync(user, request.CurrentPassword, request.NewPassword)
                                                 .ConfigureAwait(false);

                identityResult.ThrowIfFailed();

                return(Unit.Value);
            }
Пример #6
0
            public async Task <Unit> Handle(UpdateRoleCommand request, CancellationToken cancellationToken)
            {
                ApplicationRole roleToUpdate = await _roleStorage.FindByIdAsync(request.RoleId)
                                               .ConfigureAwait(false)
                                               ?? throw new NotFoundException(nameof(ApplicationRole), request.RoleId);

                UpdateApplicationRoleProperties(roleToUpdate, request);

                IIdentityResult identityResult = await _roleStorage.UpdateAsync(roleToUpdate)
                                                 .ConfigureAwait(false);

                identityResult.ThrowIfFailed();

                return(Unit.Value);
            }
Пример #7
0
            public async Task <Unit> Handle(RevokeTokenCommand request, CancellationToken cancellationToken)
            {
                RefreshToken refreshToken =
                    await _identityDbContext.RefreshTokens
                    .SingleOrDefaultAsync(rf => rf.Token == request.RefreshToken, cancellationToken)
                    .ConfigureAwait(false)
                    ?? throw new NotFoundException(nameof(Identity.Entities.RefreshToken), request.RefreshToken);

                IIdentityResult identityResult = await _jwtService.RevokeRefreshTokenAsync(refreshToken)
                                                 .ConfigureAwait(false);

                identityResult.ThrowIfFailed();

                return(Unit.Value);
            }
Пример #8
0
            public async Task <Unit> Handle(DeleteRoleOfUserCommand request, CancellationToken cancellationToken)
            {
                ApplicationUser user = await _userStorage.FindByIdAsync(request.UserId)
                                       .ConfigureAwait(false)
                                       ?? throw new NotFoundException(nameof(ApplicationUser), request.UserId);

                ApplicationRole role = await _roleStorage.FindByIdAsync(request.RoleId)
                                       .ConfigureAwait(false)
                                       ?? throw new NotFoundException(nameof(ApplicationRole), request.RoleId);

                IIdentityResult identityResult = await _userRoleService.RemoveUserFromRoleAsync(user, role)
                                                 .ConfigureAwait(false);

                identityResult.ThrowIfFailed();

                return(Unit.Value);
            }