-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated for integration with demo identity server using recommended p…
…ractices
- Loading branch information
1 parent
238ea80
commit 6aa84a6
Showing
5 changed files
with
89 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
AspNetCore-Effective-Logging/BookClub.API/SwaggerConfig.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using IdentityModel.Client; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace BookClub.API | ||
{ | ||
public class SwaggerConfig : IConfigureOptions<SwaggerGenOptions> | ||
{ | ||
private readonly IConfiguration _config; | ||
|
||
public SwaggerConfig(IConfiguration config) | ||
{ | ||
_config = config; | ||
} | ||
public void Configure(SwaggerGenOptions options) | ||
{ | ||
var disco = GetDiscoveryDocument(); | ||
var oauthScopeDic = new Dictionary<string, string> { { "api", "Access to the Book Club API" } }; | ||
|
||
//options.OperationFilter<AuthorizeOperationFilter>(); | ||
options.DescribeAllParametersInCamelCase(); | ||
options.CustomSchemaIds(x => x.FullName); | ||
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Book Club API", Version = "v1" }); | ||
|
||
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme | ||
{ | ||
Type = SecuritySchemeType.OAuth2, | ||
Flows = new OpenApiOAuthFlows | ||
{ | ||
AuthorizationCode = new OpenApiOAuthFlow | ||
{ | ||
AuthorizationUrl = new Uri(disco.AuthorizeEndpoint), | ||
TokenUrl = new Uri(disco.TokenEndpoint), | ||
Scopes = oauthScopeDic | ||
} | ||
} | ||
}); | ||
options.AddSecurityRequirement(new OpenApiSecurityRequirement | ||
{ | ||
{ | ||
new OpenApiSecurityScheme | ||
{ | ||
Reference = new OpenApiReference {Type = ReferenceType.SecurityScheme, Id = "oauth2"} | ||
}, | ||
oauthScopeDic.Keys.ToArray() | ||
} | ||
}); | ||
} | ||
|
||
private DiscoveryDocumentResponse GetDiscoveryDocument() | ||
{ | ||
var client = new HttpClient(); | ||
var authority = _config.GetValue<string>("Security:Authority"); | ||
return client.GetDiscoveryDocumentAsync(authority) | ||
.GetAwaiter() | ||
.GetResult(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters