public async Task <JsonWebToken> Handle(SignInUserCommand request, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(request.EmailOrUserName) || string.IsNullOrWhiteSpace(request.Password)) { throw new Exception("Invalid credentials."); } ApplicationUser user = await _userManagerService.GetUserByEmailOrUserName(request.EmailOrUserName); if (user == null || await _userManagerService.ValidatePasswordAsync(user, request.Password) == false) { throw new Exception("Invalid credentials."); } var refreshToken = new RefreshToken <Guid>(user.Id, 4); var roles = (await _userManagerService.GetRoles(user.Id)).ToImmutableList(); var jwt = _jwtService.GenerateToken(user.Id.ToString(), user.UserName, roles, GetCustomClaimsForUser(user.Id)); jwt.RefreshToken = refreshToken.Token; await _dataContext.Set <RefreshToken <Guid> >().AddAsync(refreshToken, cancellationToken); await _dataContext.SaveChangesAsync(cancellationToken); await _mediator.Publish(new UserLoggedInEvent(user.Id), cancellationToken); _logger.Log(LogLevel.Debug, "UserLoggedIn Event Published."); return(jwt); }
public async Task <JsonWebToken> Handle(RefreshAccessTokenCommand request, CancellationToken cancellationToken) { var refreshToken = await _dataContext.Set <RefreshToken>() .SingleOrDefaultAsync(x => x.Token == request.Token, cancellationToken); if (refreshToken == null) { throw new Exception("Refresh token not found."); } refreshToken.ValidateRefreshToken(); var user = await _userManagerService.GetUserById(refreshToken.UserId); if (user == null) { throw new Exception($"User: '******' not found."); } var newRefreshToken = new RefreshToken(user.Id, 4); try { _dataContext.BeginTransaction(); _dataContext.Set <RefreshToken>().Remove(refreshToken); await _dataContext.Set <RefreshToken>().AddAsync(newRefreshToken, cancellationToken); await _dataContext.SaveChangesAsync(cancellationToken); _dataContext.Commit(); } catch { _dataContext.Rollback(); throw; } var claims = GetCustomClaimsForUser(user.Id); var roles = (await _userManagerService.GetRoles(user.Id)).ToImmutableList(); var jwt = _jwtService.GenerateToken(user.Id.ToString("N"), user.UserName, roles, claims); jwt.RefreshToken = newRefreshToken.Token; await _mediator.Publish(new AccessTokenRefreshedEvent(user.Id, jwt.AccessToken, newRefreshToken.Token), cancellationToken); _logger.Log(LogLevel.Debug, "AccessTokenRefreshed Event Published."); return(jwt); }
public Task <IEnumerable <string> > Handle(UserRolesQuery request, CancellationToken cancellationToken) { return(_userManagerService.GetRoles(request.UserId)); }
public UserManagerViewModel GetUsers(ContextModel context) { var userManager = new UserManagerViewModel(); string usernameCustomer = null; if (context.User.Role != enRole.Administrator && context.User.Role != enRole.Demo) { usernameCustomer = context.User.Username; } var usersModel = _userManagerService.GetUsers(usernameCustomer); if (context.User.Role == enRole.Demo) { usersModel = _userManagerService.FilterRoleUsers(context.User.Role, usersModel); } // users userManager.users = usersModel.Select(s => new UserViewModel { ID = s.ID, Username = s.Username, FirstName = s.FirstName, LastName = s.LastName, Email = s.Email, CustomerName = s.CustomerName, RoleCode = (int)s.Role, RoleName = LocalizationService.GetResource(s.Role.GetDescription()), LanguageId = s.Language.ID, LanguageName = s.Language.Name, Enabled = s.Enabled, MachineSerials = s.Machines.Select(sel => sel.Serial).ToList(), TimeZone = s.TimeZone }).ToList(); //roles var rolesModel = _userManagerService.GetRoles(); userManager.roles = rolesModel.Select(s => new UserRoleViewModel { Code = s.Code, Name = LocalizationService.GetResource(s.Name), status = s.status, enabled = s.enabled }).ToList(); //customers userManager.customers = _userManagerService.GetCustomerNames(); //machines if (context.User.Role == enRole.Customer) { var machinesModel = _userManagerService.GetCustomerMachines(usernameCustomer); userManager.machines = machinesModel.Select(s => new UserMachineViewModel { Id = s.Id, Serial = s.Serial, MachineName = s.MachineName }).ToList(); } //languages var languagesModel = _userManagerService.GetLanguages(); userManager.languages = languagesModel.Select(s => new UserLanguageViewModel { Id = s.ID, Name = s.Name }).ToList(); return(userManager); }