using PartnerGateway.Api.Authentication;
using PartnerGateway.Api.Configuration;
using PartnerGateway.Api.DelegatingHandlers;
using PartnerGateway.Api.EventsHandler;
using Infrastructure.Core;
using Infrastructure.Core.Utilities;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using System;
using System.Security.Cryptography;
using System.Threading.Tasks;
using PartnerGateway.Api.Middlewares;
using Microsoft.Extensions.Hosting;
namespace PartnerGateway.Api
{
public class Startup
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="configuration"></param>
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
/// <summary>
/// This method gets called by the runtime. Use this method to add services to the container.
/// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
/// </summary>
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.Configure<AppSettings>(Configuration.GetSection(nameof(AppSettings)));
services.AddAuthentication(options =>
{
options.DefaultScheme = "SalePlatformAuth";
}).AddJwtBearer("SalePlatformAuth", options =>
{
var publicKey = Convert.FromBase64String(CoreUtility.Settings.Security.JwtRsaPublicKey);
RSA rsa = RSA.Create();
rsa.ImportSubjectPublicKeyInfo(publicKey, out _);
var securityKey = new RsaSecurityKey(rsa);
// options.IncludeErrorDetails = true;
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = Consts.Consts.JwtIssuer,
ValidateAudience = false,
ValidateIssuerSigningKey = true,
ValidateLifetime = true,
IssuerSigningKey = securityKey,
ClockSkew = TimeSpan.Zero,
};
options.Events = new JwtBearerEvents()
{
OnTokenValidated = JwtEventsHandler.OnTokenValidatedHandler(),
};
});
services.AddOcelot(Configuration)
.AddDelegatingHandler<DelegatingHandlersCustomize>(true);
}
/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.AddCoreInfrastructureLayer(env);
AppSettingServices.Services = app.ApplicationServices;
app.UsePathBase(new PathString("/partner-gw"));
app.Use(async (context, next) =>
{
context.Response.OnStarting(() =>
{
context.Response.Headers["spf-response"] = "spf-internal-gw";
return Task.CompletedTask;
});
await next();
});
if (!env.IsDevelopment())
{
app.UseMiddleware<CheckSumMiddleware>();
}
app.UseAuthentication();
var configuration = new OcelotPipelineConfiguration
{
PreErrorResponderMiddleware = CustomMiddlewareHandler.PreErrorResponseHandler(),
};
app.UseOcelot(configuration).Wait();
}
}
}
Nhận xét
Đăng nhận xét