1414
1515namespace SpaceAPI . Host
1616{
17- public class Startup
17+ public static class WebApplicationBuilder
1818 {
19- public Startup ( IConfiguration configuration )
20- {
21- Configuration = configuration ;
22- }
23-
24- public IConfiguration Configuration { get ; }
2519
2620 // This method gets called by the runtime. Use this method to add services to the container.
27- public void ConfigureServices ( IServiceCollection services )
21+ public static void ConfigureServices ( IServiceCollection services , IConfiguration configuration )
2822 {
2923 var version = GetVersion ( ) ;
3024
31- var configurationSection = Configuration . GetSection ( nameof ( AuthConfig ) ) ;
25+ var configurationSection = configuration . GetSection ( nameof ( AuthConfig ) ) ;
3226 var authConfiguration = configurationSection . Get < AuthConfig > ( ) ;
3327 services . Configure < AuthConfig > ( configurationSection ) ;
3428
3529 services . AddSingleton < IHttpContextAccessor , HttpContextAccessor > ( ) ;
3630 services . AddControllers ( options =>
3731 {
3832 options . RespectBrowserAcceptHeader = true ;
39- } ) ;
40-
41- services . AddAuthentication ( authOptions =>
42- {
43- authOptions . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
44- authOptions . DefaultChallengeScheme = JwtBearerDefaults . AuthenticationScheme ;
45- authOptions . DefaultScheme = JwtBearerDefaults . AuthenticationScheme ;
46- } ) . AddJwtBearer ( options =>
33+ } ) . AddJsonOptions ( o =>
4734 {
48- options . Authority = authConfiguration . Authority ;
49- options . Audience = authConfiguration . Audience ;
50- //options.SaveToken = true;
51- options . RequireHttpsMetadata = false ;
52- options . TokenValidationParameters = new TokenValidationParameters ( )
53- {
54- ValidateAudience = false
55- } ;
35+ o . JsonSerializerOptions . DefaultIgnoreCondition = System . Text . Json . Serialization . JsonIgnoreCondition . WhenWritingNull ;
5636 } ) ;
5737
58- services . AddAuthorization ( auth =>
59- {
60- var defaultPolicy =
61- new AuthorizationPolicyBuilder ( )
62- . AddAuthenticationSchemes ( JwtBearerDefaults . AuthenticationScheme )
63- . RequireAuthenticatedUser ( )
64- . Build ( ) ;
6538
66- auth . DefaultPolicy = defaultPolicy ;
39+ AddAuthentication ( services , authConfiguration ) ;
40+ AddAuthorization ( services ) ;
41+ AddCors ( services ) ;
42+ AddOpenAPI ( services , version ) ;
43+
44+ ConfigureVerticals ( services , configuration ) ;
45+ }
46+
47+ private static void AddOpenAPI ( IServiceCollection services , string version )
48+ {
49+ services . AddSwaggerGen ( options =>
50+ {
51+ options . SwaggerDoc ( $ "v{ version } ", new OpenApiInfo ( )
52+ {
53+ Version = version ,
54+ Title = "Brixel.SpaceAPI" ,
55+ Description = "SpaceAPI of Brixel"
56+ } ) ;
57+ options . DocumentFilter < AdditionalParametersDocumentFilter > ( ) ;
58+ options . CustomOperationIds (
59+ d => ( d . ActionDescriptor as ControllerActionDescriptor ) ? . ActionName ) ;
6760 } ) ;
61+ }
6862
63+ private static void AddCors ( IServiceCollection services )
64+ {
6965 services . AddCors ( options =>
7066 {
7167 options . AddDefaultPolicy (
@@ -77,22 +73,40 @@ public void ConfigureServices(IServiceCollection services)
7773 . AllowAnyHeader ( ) ;
7874 } ) ;
7975 } ) ;
76+ }
8077
81- services . AddSwaggerGen ( options =>
78+ private static void AddAuthorization ( IServiceCollection services )
79+ {
80+ services . AddAuthorization ( auth =>
8281 {
83- options . SwaggerDoc ( $ "v{ version } ", new OpenApiInfo ( )
82+ var defaultPolicy =
83+ new AuthorizationPolicyBuilder ( )
84+ . AddAuthenticationSchemes ( JwtBearerDefaults . AuthenticationScheme )
85+ . RequireAuthenticatedUser ( )
86+ . Build ( ) ;
87+
88+ auth . DefaultPolicy = defaultPolicy ;
89+ } ) ;
90+ }
91+
92+ private static void AddAuthentication ( IServiceCollection services , AuthConfig authConfiguration )
93+ {
94+ services . AddAuthentication ( authOptions =>
95+ {
96+ authOptions . DefaultAuthenticateScheme = JwtBearerDefaults . AuthenticationScheme ;
97+ authOptions . DefaultChallengeScheme = JwtBearerDefaults . AuthenticationScheme ;
98+ authOptions . DefaultScheme = JwtBearerDefaults . AuthenticationScheme ;
99+ } ) . AddJwtBearer ( options =>
100+ {
101+ options . Authority = authConfiguration . Authority ;
102+ options . Audience = authConfiguration . Audience ;
103+ //options.SaveToken = true;
104+ options . RequireHttpsMetadata = false ;
105+ options . TokenValidationParameters = new TokenValidationParameters ( )
84106 {
85- Version = version ,
86- Title = "Brixel.SpaceAPI" ,
87- Description = "SpaceAPI of Brixel"
88- } ) ;
89- options . DocumentFilter < AdditionalParametersDocumentFilter > ( ) ;
90- options . CustomOperationIds (
91- d => ( d . ActionDescriptor as ControllerActionDescriptor ) ? . ActionName ) ;
107+ ValidateAudience = false
108+ } ;
92109 } ) ;
93- services . AddMvcCore ( )
94- . AddApiExplorer ( ) ;
95- ConfigureVerticals ( services , Configuration ) ;
96110 }
97111
98112 private static string GetVersion ( )
@@ -101,13 +115,13 @@ private static string GetVersion()
101115
102116 }
103117
104- private void ConfigureVerticals ( IServiceCollection serviceCollection , IConfiguration configuration )
118+ private static void ConfigureVerticals ( IServiceCollection serviceCollection , IConfiguration configuration )
105119 {
106120 Bootstrapper . Configure ( serviceCollection , configuration ) ;
107121 }
108122
109123 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
110- public void Configure ( IApplicationBuilder app , IWebHostEnvironment env )
124+ public static void Configure ( IApplicationBuilder app , IWebHostEnvironment env )
111125 {
112126 if ( env . IsDevelopment ( ) )
113127 {
0 commit comments