private async Task attachUser(HttpContext context, string token, IAlphahomeService service) { try { var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.UTF8.GetBytes(_configuration["Jwt:Key"]); tokenHandler.ValidateToken(token, new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = false, ValidateAudience = false, ClockSkew = TimeSpan.Zero }, out SecurityToken validatedToken); var jwtToken = (JwtSecurityToken)validatedToken; var userId = (jwtToken.Claims.First(x => x.Type == "sub").Value); if (!string.IsNullOrEmpty(userId)) { // Guid.TryParse(userId, out var guidId); var user = await service.FindUserAsync(userId); if (user.valid) { //await _next.Invoke(context); context.Items["Account"] = userId; } else { //await _next(null); } } else { //await _next(null); } } catch (Exception ex) { throw ex; } }