private async Task attachAccountToContext(HttpContext context, FlightsManagerDb dataContext, string token) { try { var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_appSettings.Secret); tokenHandler.ValidateToken(token, new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = false, ValidateAudience = false, // set clockskew to zero so tokens expire exactly at token expiration time (instead of 5 minutes later) ClockSkew = TimeSpan.Zero }, out SecurityToken validatedToken); var jwtToken = (JwtSecurityToken)validatedToken; var accountId = int.Parse(jwtToken.Claims.First(x => x.Type == "id").Value); // attach account to context on successful jwt validation context.Items["Account"] = await dataContext.Accounts.FindAsync(accountId); } catch { // do nothing if jwt validation fails // account is not attached to context so request won't have access to secure routes } }
public AccountService(FlightsManagerDb context, IOptions <AppSettings> appSettings, IEmailService emailService) { this.context = context; this.appSettings = appSettings.Value; this.emailService = emailService; }
public async Task Invoke(HttpContext context, FlightsManagerDb dataContext) { var token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last(); if (token != null) { await attachAccountToContext(context, dataContext, token); } await _next(context); }
public AmadeusService(HttpClient client, FlightsManagerDb context) { clientId = context.CustomParameters.Where(p => p.Key == "client_id").FirstOrDefault(); clientSecret = context.CustomParameters.Where(p => p.Key == "client_secret").FirstOrDefault(); client.BaseAddress = new Uri("https://test.api.amadeus.com"); this.client = client; var authorizationResponse = Authorize(); bearerToken = authorizationResponse.Result.access_token; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken); }
public AdministratorController(FlightsManagerDb context) { this.context = context; }
public FlightManagerService(FlightsManagerDb context) { this.context = context; }