public async Task Invoke(HttpContext context, IAlphahomeService service) { var token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last(); if (context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").First() != "Basic") { if (token != null) { await attachUser(context, token, service); } } else { } // context.Request.Headers.Clear(); await _next(context); }
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; } }
public ManagerController(IAlphahomeService alphahomeService, IWebHostEnvironment environment) { _alphahomeService = alphahomeService; _env = environment; }
public ImageLibController(IAlphahomeService alphahomeService) { _alphahomeService = alphahomeService; }
public AlphahomeController(IAlphahomeService alphahomeService) { _alphahomeService = alphahomeService; }
public UserController(IAlphahomeService alphahomeService, IConfiguration config) { _alphahomeService = alphahomeService; _config = config; }
public UploadController(IAlphahomeService alphahomeService) { _alphahomeService = alphahomeService; }