public async Task InvokeAsync(HttpContext context) { Common.ApiClient.Models.UserProfile apiUserProfile = _userProfileService.GetUser(); UserProfile userProfile = new UserProfile(string.IsNullOrWhiteSpace(apiUserProfile.Id) ? "testid" : apiUserProfile.Id, string.IsNullOrWhiteSpace(apiUserProfile.Fullname) ? "testuser" : apiUserProfile.Fullname); _userProfileProvider.UserProfile = userProfile; // Call the next delegate/middleware in the pipeline await this._next(context); }
private static void SetDefaultApiClientConfigurationOptions(HttpClient httpClient, ApiClientConfigurationOptions options, IServiceCollection services) { Guard.ArgumentNotNull(httpClient, nameof(httpClient)); Guard.ArgumentNotNull(options, nameof(options)); Guard.ArgumentNotNull(services, nameof(services)); if (string.IsNullOrWhiteSpace(options.ApiEndpoint)) { throw new InvalidOperationException("options EndPoint is null or empty string"); } string baseAddress = options.ApiEndpoint; if (!baseAddress.EndsWith("/", StringComparison.CurrentCulture)) { baseAddress = $"{baseAddress}/"; } IServiceProvider serviceProvider = services.BuildServiceProvider(); IUserProfileService userProfileService = serviceProvider.GetService <IUserProfileService>(); Common.ApiClient.Models.UserProfile userProfile = userProfileService.GetUser(); httpClient.BaseAddress = new Uri(baseAddress, UriKind.Absolute); httpClient.DefaultRequestHeaders?.Add(ApiClientHeaders.ApiKey, options.ApiKey); if (string.IsNullOrEmpty(userProfile.Fullname) || string.IsNullOrEmpty(userProfile.Id)) { httpClient.DefaultRequestHeaders?.Add(ApiClientHeaders.Username, "testuser"); httpClient.DefaultRequestHeaders?.Add(ApiClientHeaders.UserId, "testid"); } else { httpClient.DefaultRequestHeaders?.Add(ApiClientHeaders.Username, userProfile.Fullname); httpClient.DefaultRequestHeaders?.Add(ApiClientHeaders.UserId, userProfile.Id); } httpClient.DefaultRequestHeaders?.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); httpClient.DefaultRequestHeaders?.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip")); httpClient.DefaultRequestHeaders?.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate")); }