private AuthResultModel GenerateAuthResultForUser(UserModel userModel) { var key = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_jwtSettings.Secret)); var tokenHandler = new JwtSecurityTokenHandler(); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new[] { new Claim("UserName", userModel.UserName), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), new Claim(JwtRegisteredClaimNames.Email, userModel.Email), new Claim("Role", userModel.Role), new Claim("id", userModel.Id), }), Expires = DateTime.UtcNow.AddHours(2), SigningCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); return(new AuthResultModel { Token = tokenHandler.WriteToken(token), Success = true, UserFirstName = userModel.FirstName, UserLastName = userModel.LastName, UserMiddleName = userModel.MiddleName, UserPosition = _positionManager.GetPositionByIDAsync(userModel.PositionId).Result.PositionName, AvatarImage = userModel.AvatarImage, UserRole = userModel.Role, Username = userModel.UserName, }); }
public async Task <List <ResponseUserModel> > GetInteractedUsersAsync(string username) { if (await _userManager.FindByNameAsync(username) == null) { return(null); } List <ResponseUserModel> resultUsers = new List <ResponseUserModel>(); var interectedUsers = _dataContext.interectedUsers.Where(x => x.UserName == username).ToList(); foreach (var elemet in interectedUsers) { var userModel = await _userManager.FindByNameAsync(elemet.InterectedUserName); if (userModel != null) { resultUsers.Add(new ResponseUserModel { FirstName = userModel.FirstName, LastName = userModel.LastName, MiddleName = userModel.MiddleName, Position = _positionManager.GetPositionByIDAsync(userModel.PositionId).Result.PositionName, Email = userModel.Email, AvatarImage = userModel.AvatarImage, UserRole = userModel.Role, Username = userModel.UserName, }); } } if (resultUsers.Count > 0) { return(resultUsers); } else { return(null); } }