/// <summary> /// Initializes a new instance of the <see cref="PharmanetDelegate"/> class. /// </summary> /// <param name="client">The injected HttpClient from service injection.</param> /// <param name="logger">Injected Logger Provider.</param> /// <param name="configuration">The injected configuration provider.</param> public PharmanetDelegate(HttpClient client, ILogger <PharmanetDelegate> logger, IConfiguration configuration) { this.httpClient = client; this.logger = logger; this.configuration = configuration; this.pharmanetDelegateConfig = new PharmanetDelegateConfig(); this.configuration.Bind(PharmanetDelegateConfig.ConfigurationSectionKey, this.pharmanetDelegateConfig); }
/// <summary> /// This method gets called by the runtime. Use this method to add services to the container. /// </summary> /// <param name="services">The injected services provider.</param> public void ConfigureServices(IServiceCollection services) { this.startupConfig.ConfigureForwardHeaders(services); this.startupConfig.ConfigureHttpServices(services); this.startupConfig.ConfigureAuthServicesForJwtBearer(services); this.startupConfig.ConfigureSwaggerServices(services); services.AddAuthorization(options => { string claimsIssuer = this.configuration.GetSection(ConfigurationSections.OpenIdConnect).GetValue <string>("ClaimsIssuer"); string scopes = this.configuration.GetSection(ConfigurationSections.OpenIdConnect).GetValue <string>("Scope"); string[] scope = scopes.Split(" ", StringSplitOptions.RemoveEmptyEntries); options.AddPolicy(Hl7v2ScopesPolicy.MessageTypeScopeAccess, policy => { policy.Requirements.Add(new Hl7v2AuthorizationRequirement(this.configuration)); }); }); services.AddSingleton <IAuthorizationHandler, Hl7v2AuthorizationHandler>(); services.AddCors(options => { options.AddPolicy("allowAny", policy => { policy .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); }); }); PharmanetDelegateConfig pharmanetDelegateConfig = new PharmanetDelegateConfig(); this.configuration.Bind("PharmanetProxy", pharmanetDelegateConfig); // Add http client services at ConfigureServices(IServiceCollection services). services.AddHttpClient <IPharmanetDelegate, PharmanetDelegate>("PharmanetProxyClient", c => { c.BaseAddress = new Uri(pharmanetDelegateConfig.Endpoint); c.DefaultRequestHeaders.Accept.Clear(); c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json)); byte[] authdata = Encoding.ASCII.GetBytes(pharmanetDelegateConfig.Username + ":" + pharmanetDelegateConfig.Password); c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", System.Convert.ToBase64String(authdata)); }).ConfigurePrimaryHttpMessageHandler(() => { HttpClientHandler handler = new HttpClientHandler(); // Load Certificate, if non-empty in Configuration. string certPath = pharmanetDelegateConfig.ClientCertificatePath; string certPassword = pharmanetDelegateConfig.ClientCertificatePassword; if (!string.IsNullOrEmpty(certPath)) { handler.ClientCertificates.Add(new X509Certificate2(System.IO.File.ReadAllBytes(certPath), certPassword)); } return(handler); }); // Add services. services.AddTransient <IPharmanetService, PharmanetService>(); services.AddTransient <IHl7Parser, Hl7Parser>(); }