Skip to content

Commit 6aa84a6

Browse files
updated for integration with demo identity server using recommended practices
1 parent 238ea80 commit 6aa84a6

File tree

5 files changed

+89
-40
lines changed

5 files changed

+89
-40
lines changed

AspNetCore-Effective-Logging/BookClub.API/BookClub.API.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
8+
<PackageReference Include="IdentityModel" Version="4.3.1" />
9+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.6" />
910
<PackageReference Include="NLog.Targets.ElasticSearch" Version="7.3.0" />
1011
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.2" />
1112
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />

AspNetCore-Effective-Logging/BookClub.API/Startup.cs

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Data;
43
using System.Data.SqlClient;
5-
using System.Linq;
64
using BookClub.Infrastructure.Middleware;
75
using BookClub.Data;
86
using BookClub.Logic;
@@ -12,11 +10,11 @@
1210
using Microsoft.AspNetCore.Mvc.Authorization;
1311
using Microsoft.Extensions.Configuration;
1412
using Microsoft.Extensions.DependencyInjection;
15-
using Swashbuckle.AspNetCore.Swagger;
1613
using BookClub.Infrastructure.Filters;
1714
using BookClub.Infrastructure;
1815
using Microsoft.Extensions.Logging;
19-
using Microsoft.OpenApi.Models;
16+
using Microsoft.Extensions.Options;
17+
using Swashbuckle.AspNetCore.SwaggerGen;
2018

2119
namespace 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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net.Http;
5+
using IdentityModel.Client;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Options;
9+
using Microsoft.OpenApi.Models;
10+
using Swashbuckle.AspNetCore.SwaggerGen;
11+
12+
namespace BookClub.API
13+
{
14+
public class SwaggerConfig : IConfigureOptions<SwaggerGenOptions>
15+
{
16+
private readonly IConfiguration _config;
17+
18+
public SwaggerConfig(IConfiguration config)
19+
{
20+
_config = config;
21+
}
22+
public void Configure(SwaggerGenOptions options)
23+
{
24+
var disco = GetDiscoveryDocument();
25+
var oauthScopeDic = new Dictionary<string, string> { { "api", "Access to the Book Club API" } };
26+
27+
//options.OperationFilter<AuthorizeOperationFilter>();
28+
options.DescribeAllParametersInCamelCase();
29+
options.CustomSchemaIds(x => x.FullName);
30+
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Book Club API", Version = "v1" });
31+
32+
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
33+
{
34+
Type = SecuritySchemeType.OAuth2,
35+
Flows = new OpenApiOAuthFlows
36+
{
37+
AuthorizationCode = new OpenApiOAuthFlow
38+
{
39+
AuthorizationUrl = new Uri(disco.AuthorizeEndpoint),
40+
TokenUrl = new Uri(disco.TokenEndpoint),
41+
Scopes = oauthScopeDic
42+
}
43+
}
44+
});
45+
options.AddSecurityRequirement(new OpenApiSecurityRequirement
46+
{
47+
{
48+
new OpenApiSecurityScheme
49+
{
50+
Reference = new OpenApiReference {Type = ReferenceType.SecurityScheme, Id = "oauth2"}
51+
},
52+
oauthScopeDic.Keys.ToArray()
53+
}
54+
});
55+
}
56+
57+
private DiscoveryDocumentResponse GetDiscoveryDocument()
58+
{
59+
var client = new HttpClient();
60+
var authority = _config.GetValue<string>("Security:Authority");
61+
return client.GetDiscoveryDocumentAsync(authority)
62+
.GetAwaiter()
63+
.GetResult();
64+
}
65+
}
66+
}

AspNetCore-Effective-Logging/BookClub.API/appsettings.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@
99
"AllowedHosts": "*",
1010
"ConnectionStrings": {
1111
"BookClubDb": "Server=.\\sqlexpress;Database=BookClub;Trusted_Connection=True;"
12-
}
12+
},
13+
"Security": {
14+
"Authority": "https://demo.identityserver.io",
15+
"ClientId": "interactive.confidential",
16+
"ClientSecret": "secret",
17+
"Audience": "api"
18+
}
1319
}

AspNetCore-Effective-Logging/BookClub.UI/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public void ConfigureServices(IServiceCollection services)
4545
options.SignInScheme = "Cookies";
4646
options.Authority = "https://demo.identityserver.io";
4747

48-
options.ClientId = "server.hybrid";
48+
options.ClientId = "interactive.confidential";
4949
options.ClientSecret = "secret";
50-
options.ResponseType = "code id_token";
50+
options.ResponseType = "code";
5151
options.Scope.Add("email");
5252
options.Scope.Add("api");
5353
options.Scope.Add("offline_access");

0 commit comments

Comments
 (0)