protected override void ConfigureWebHost(IWebHostBuilder builder) { var conf = (IConfiguration) new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .Build(); var env = new ContextService(InstanceContext.IntegrationTest); var map = new MapperConfiguration(x => x.AddProfile <AutoMapperProfile_EFCore_TBL>()).CreateMapper(); builder.ConfigureServices(sc => { sc.AddSingleton <IConfiguration>(conf); sc.AddSingleton <IContextService>(env); sc.AddSingleton <IMapper>(map); sc.AddScoped <IUnitOfWork, UnitOfWork>(_ => { var uow = new UnitOfWork(conf["Databases:IdentityEntities_EFCore_Tbl"], env); var data = new DefaultDataFactory_Tbl(uow); data.CreateSettings(); return(uow); }); sc.AddSingleton <IDefaultDataFactory_Tbl, DefaultDataFactory_Tbl>(); sc.AddSingleton <IOAuth2JwtFactory, OAuth2JwtFactory>(); sc.AddControllers() .AddNewtonsoftJson(opt => { opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); }) //https://github.com/aspnet/Mvc/issues/5992 .AddApplicationPart(typeof(BaseController).Assembly); }); builder.Configure(app => { }); }
protected override void ConfigureWebHost(IWebHostBuilder builder) { var conf = (IConfiguration) new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .Build(); var env = new ContextService(InstanceContext.SystemTest); var map = new MapperConfiguration(x => x.AddProfile <AutoMapperProfile_EFCore_TBL>()) .CreateMapper(); builder.ConfigureServices(sc => { sc.AddSingleton <IConfiguration>(conf); sc.AddSingleton <IContextService>(env); sc.AddSingleton <IMapper>(map); sc.AddSingleton <IAuthorizationHandler, IdentityUsersAuthorize>(); sc.AddSingleton <IAuthorizationHandler, IdentityServicesAuthorize>(); sc.AddScoped <IUnitOfWork, UnitOfWork>(_ => { var uow = new UnitOfWork(conf["Databases:IdentityEntities_EFCore_Tbl"], env); var data = new DefaultDataFactory_Tbl(uow); data.CreateSettings(); data.CreateAudienceRoles(); data.CreateUserLogins(); data.CreateUserRoles(); return(uow); }); sc.AddSingleton <IOAuth2JwtFactory, OAuth2JwtFactory>(); /* * do not use dependency inject for unit of work below. is used * only for owin authentication configuration. */ var owin = new UnitOfWork(conf["Databases:IdentityEntities_EFCore_Tbl"], env); var data = new DefaultDataFactory_Tbl(owin); data.CreateIssuers(); data.CreateAudiences(); var issuers = owin.Issuers.Get() .Select(x => x.Name + ":" + conf["IdentityTenant:Salt"]); var issuerKeys = owin.Issuers.Get() .Select(x => x.IssuerKey); var audiences = owin.Audiences.Get() .Select(x => x.Name); sc.AddControllers() .AddNewtonsoftJson(opt => { opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); }) //https://github.com/aspnet/Mvc/issues/5992 .AddApplicationPart(typeof(BaseController).Assembly); sc.AddCors(); sc.AddAuthentication(opt => { opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; opt.DefaultForbidScheme = JwtBearerDefaults.AuthenticationScheme; opt.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; opt.DefaultSignOutScheme = JwtBearerDefaults.AuthenticationScheme; opt.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(jwt => { jwt.IncludeErrorDetails = true; jwt.TokenValidationParameters = new TokenValidationParameters { ValidIssuers = issuers.ToArray(), IssuerSigningKeys = issuerKeys.Select(x => new SymmetricSecurityKey(Encoding.Unicode.GetBytes(x))).ToArray(), ValidAudiences = audiences.ToArray(), AudienceValidator = AudiencesValidator.Multiple, ValidateIssuer = true, ValidateAudience = true, ValidateIssuerSigningKey = true, ValidateLifetime = true, RequireAudience = true, RequireExpirationTime = true, RequireSignedTokens = true, }; }); sc.AddAuthorization(opt => { opt.AddPolicy(DefaultConstants.OAuth2ROPGrants, humans => { humans.Requirements.Add(new IdentityUsersAuthorizeRequirement()); }); opt.AddPolicy(DefaultConstants.OAuth2CCGrants, servers => { servers.Requirements.Add(new IdentityServicesAuthorizeRequirement()); }); }); sc.AddSwaggerGen(opt => { opt.SwaggerDoc("v1", new OpenApiInfo { Title = "Reference", Version = "v1" }); }); sc.Configure <ForwardedHeadersOptions>(opt => { opt.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; }); }); builder.Configure(app => { app.UseForwardedHeaders(); app.UseStaticFiles(); app.UseSwagger(opt => { opt.RouteTemplate = "help/{documentName}/index.json"; }); app.UseSwaggerUI(opt => { opt.RoutePrefix = "help"; opt.SwaggerEndpoint("v1/index.json", "Reference"); }); app.UseRouting(); app.UseCors(opt => opt .AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod()); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(opt => { opt.MapControllers(); }); }); }