public async Task <string> GenerateAccessToken(Person person, string activeRole) { var storedUserRoles = await _customUserRolesService.GatherAllUserRoles(person.Id); var claims = new List <Claim>(); claims.Add(new Claim("UserId", person.Id.ToString())); claims.Add(new Claim(ClaimTypes.Role, activeRole)); storedUserRoles.ForEach(role => claims.Add(new Claim("User roles", role))); var token = new JwtSecurityToken( claims: claims, expires: acccessTokenExpirationTime, signingCredentials: _credentials); return(new JwtSecurityTokenHandler().WriteToken(token)); }
public async Task <ActionResult <Person> > LoginUser(PersonLoginDTO current) { var storedPerson = _repository.GetByEmail(current.Email); if (storedPerson == null) { return(Unauthorized(UnauthorizedEmptyJsonResult)); } //TODO usunąć gdy Frontend przejdzie na używanie ról z Access Tokenu. var storedUserRoles = await _customUserRolesService.GatherAllUserRoles(storedPerson.Id); var token = await _jwtService.GenerateAccessToken(storedPerson, storedUserRoles.First()); var refreshToken = _jwtService.GenerateRefreshToken(); try { UpdateRefreshToken(storedPerson, refreshToken); return(Accepted(new { Id = storedPerson.Id, FirstName = storedPerson.Name, LastName = storedPerson.Lastname, Email = storedPerson.Email, Roles = storedUserRoles, Token = token, RefreshToken = refreshToken })); } catch (DBConcurrencyException e) { return(Conflict(ConflictJsonResult(e.Message))); } }
public async Task <ActionResult <IEnumerable <String> > > GetNonStandard(decimal PersonId) { return(Ok(_rolesService.GatherAllUserRoles(PersonId))); }