11using System ;
2- using System . Collections . Generic ;
32using System . Data ;
43using System . Data . SqlClient ;
5- using System . Linq ;
64using BookClub . Infrastructure . Middleware ;
75using BookClub . Data ;
86using BookClub . Logic ;
1210using Microsoft . AspNetCore . Mvc . Authorization ;
1311using Microsoft . Extensions . Configuration ;
1412using Microsoft . Extensions . DependencyInjection ;
15- using Swashbuckle . AspNetCore . Swagger ;
1613using BookClub . Infrastructure . Filters ;
1714using BookClub . Infrastructure ;
1815using Microsoft . Extensions . Logging ;
19- using Microsoft . OpenApi . Models ;
16+ using Microsoft . Extensions . Options ;
17+ using Swashbuckle . AspNetCore . SwaggerGen ;
2018
2119namespace BookClub . API
2220{
@@ -40,44 +38,19 @@ public void ConfigureServices(IServiceCollection services)
4038 services . AddScoped < IDbConnection , SqlConnection > ( p =>
4139 new SqlConnection ( Configuration . GetConnectionString ( "BookClubDb" ) ) ) ;
4240 services . AddScoped < IBookRepository , BookRepository > ( ) ;
43- services . AddScoped < IBookLogic , BookLogic > ( ) ;
41+ services . AddScoped < IBookLogic , BookLogic > ( ) ;
42+ services . AddTransient < IConfigureOptions < SwaggerGenOptions > , SwaggerConfig > ( ) ;
4443
4544 services . AddAuthentication ( "Bearer" )
46- . AddIdentityServerAuthentication ( options =>
45+ . AddJwtBearer ( options =>
4746 {
48- options . Authority = "https://demo.identityserver.io" ;
49- options . ApiName = "api" ;
47+ options . Authority = Configuration . GetValue < string > ( "Security:Authority" ) ;
48+ options . Audience = Configuration . GetValue < string > ( "Security:Audience" ) ;
5049 } ) ;
5150
5251 services . AddAuthorization ( ) ;
5352
54- services . AddSwaggerGen ( c =>
55- {
56- var oauthScopeDic = new Dictionary < string , string > { { "api" , "Access to the Book Club API" } } ;
57- c . SwaggerDoc ( "v1" , new OpenApiInfo { Title = "Book Club API" , Version = "v1" } ) ;
58- c . AddSecurityDefinition ( "oauth2" , new OpenApiSecurityScheme
59- {
60- Type = SecuritySchemeType . OAuth2 ,
61- Flows = new OpenApiOAuthFlows
62- {
63- Implicit = new OpenApiOAuthFlow
64- {
65- AuthorizationUrl = new Uri ( "https://demo.identityserver.io/connect/authorize" ) ,
66- Scopes = oauthScopeDic
67- }
68- }
69- } ) ;
70- c . AddSecurityRequirement ( new OpenApiSecurityRequirement
71- {
72- {
73- new OpenApiSecurityScheme
74- {
75- Reference = new OpenApiReference { Type = ReferenceType . SecurityScheme , Id = "oauth2" }
76- } ,
77- oauthScopeDic . Keys . ToArray ( )
78- }
79- } ) ;
80- } ) ;
53+ services . AddSwaggerGen ( ) ; // configured in SwaggerConfig by transient dependency above
8154
8255 services . AddMvc ( options =>
8356 {
@@ -101,7 +74,10 @@ public void Configure(IApplicationBuilder app)
10174 app . UseSwaggerUI ( options =>
10275 {
10376 options . SwaggerEndpoint ( "/swagger/v1/swagger.json" , "Book Club API" ) ;
104- options . OAuthClientId ( "implicit" ) ; // should represent the swagger UI
77+ options . OAuthClientId ( Configuration . GetValue < string > ( "Security:ClientId" ) ) ;
78+ options . OAuthClientSecret ( Configuration . GetValue < string > ( "Security:ClientSecret" ) ) ;
79+ options . OAuthAppName ( "Book Club API" ) ;
80+ options . OAuthUsePkce ( ) ;
10581 } ) ;
10682 app . UseAuthentication ( ) ;
10783
0 commit comments