« Scalar » : différence entre les versions
Apparence
(5 versions intermédiaires par le même utilisateur non affichées) | |||
Ligne 4 : | Ligne 4 : | ||
* [https://learn.microsoft.com/en-us/aspnet/core/fundamentals/openapi/using-openapi-documents?view=aspnetcore-9.0#use-scalar-for-interactive-api-documentation Use Scalar for interactive API documentation] | * [https://learn.microsoft.com/en-us/aspnet/core/fundamentals/openapi/using-openapi-documents?view=aspnetcore-9.0#use-scalar-for-interactive-api-documentation Use Scalar for interactive API documentation] | ||
= [https://guides.scalar.com/scalar/scalar-api-references/net-integration | = ASP.NET Core 9 integration = | ||
* [https://github.com/scalar/scalar/blob/main/integrations/aspnetcore/README.md Read me] | |||
* [https://guides.scalar.com/scalar/scalar-api-references/net-integration Guide] | |||
* [https://github.com/scalar/scalar/blob/main/integrations/aspnetcore/CHANGELOG.md Change log] | |||
<kode lang='ps'> | <kode lang='ps'> | ||
dotnet add package Scalar.AspNetCore | dotnet add package Scalar.AspNetCore | ||
Ligne 19 : | Ligne 23 : | ||
{ | { | ||
options | options | ||
. | .WithTitle("MyApp API Reference") | ||
.WithTheme(ScalarTheme.Solarized); | .WithTheme(ScalarTheme.Solarized) | ||
.WithClientButton(false); | |||
//.WithDefaultOpenAllTags(true) | |||
}); | }); | ||
} | } | ||
Ligne 34 : | Ligne 40 : | ||
} | } | ||
} | } | ||
} | |||
</filebox> | |||
== OAuth 2 authentication == | |||
<filebox fn='OpenApiConfiguration.cs' collapsed> | |||
public static class OpenApiConfiguration | |||
{ | |||
private const string oAuth2Scheme = "oauth2"; | |||
public static IMvcBuilder ConfigureOpenApi(this IMvcBuilder builder, AzureAdConfiguration configuration) | |||
{ | |||
builder.Services.AddOpenApi(options => | |||
{ | |||
options.AddDocumentTransformer((document, context, cancellationToken) => | |||
{ | |||
document.Components ??= new OpenApiComponents(); | |||
document.Components.SecuritySchemes.Add(oAuth2Scheme, new OpenApiSecurityScheme | |||
{ | |||
Type = SecuritySchemeType.OAuth2, | |||
Flows = new OpenApiOAuthFlows | |||
{ | |||
AuthorizationCode = new OpenApiOAuthFlow | |||
{ | |||
AuthorizationUrl = new Uri($"https://login.microsoftonline.com/{configuration.TenantId}/oauth2/v2.0/authorize"), | |||
TokenUrl = new Uri($"https://login.microsoftonline.com/{configuration.TenantId}/oauth2/v2.0/token"), | |||
Scopes = new Dictionary<string, string> | |||
{ | |||
{ $"{configuration.ClientId}/contributor offline_access", "API access" } | |||
} | |||
} | |||
} | |||
}); | |||
return Task.CompletedTask; | |||
}); | |||
}); | |||
return builder; | |||
} | |||
public static void ConfigureOAuthAuthenticationForScalar(this WebApplication app, AzureAdConfiguration configuration) | |||
{ | |||
if (app.Environment.IsDevelopment()) | |||
{ | |||
app.MapScalarApiReference(options => | |||
{ | |||
options | |||
.WithTitle("MyApp API Reference") | |||
.WithTheme(ScalarTheme.Solarized) | |||
.WithClientButton(false) | |||
.WithPreferredScheme(oAuth2Scheme) | |||
.WithOAuth2Authentication(oauth => | |||
{ | |||
oauth.ClientId = configuration.SwaggerClientId; | |||
oauth.Scopes = [$"{configuration.ClientId}/contributor offline_access"]; | |||
}); | |||
}); | |||
} | |||
} | |||
} | } | ||
</filebox> | </filebox> |
Version du 3 avril 2025 à 09:56
Links
ASP.NET Core 9 integration
dotnet add package Scalar.AspNetCore |
Program.cs |
builder.Services.AddOpenApi(); if (app.Environment.IsDevelopment()) { app.MapOpenApi(); // add the following lines app.MapScalarApiReference(options => { options .WithTitle("MyApp API Reference") .WithTheme(ScalarTheme.Solarized) .WithClientButton(false); //.WithDefaultOpenAllTags(true) }); } |
URL: http://localhost:5000/scalar
Properties/launchSettings.json |
{ "profiles": { "http": { "launchBrowser": true, "launchUrl": "http://localhost:5000/scalar", "applicationUrl": "http://localhost:5000" } } } |
OAuth 2 authentication
OpenApiConfiguration.cs |
public static class OpenApiConfiguration { private const string oAuth2Scheme = "oauth2"; public static IMvcBuilder ConfigureOpenApi(this IMvcBuilder builder, AzureAdConfiguration configuration) { builder.Services.AddOpenApi(options => { options.AddDocumentTransformer((document, context, cancellationToken) => { document.Components ??= new OpenApiComponents(); document.Components.SecuritySchemes.Add(oAuth2Scheme, new OpenApiSecurityScheme { Type = SecuritySchemeType.OAuth2, Flows = new OpenApiOAuthFlows { AuthorizationCode = new OpenApiOAuthFlow { AuthorizationUrl = new Uri($"https://login.microsoftonline.com/{configuration.TenantId}/oauth2/v2.0/authorize"), TokenUrl = new Uri($"https://login.microsoftonline.com/{configuration.TenantId}/oauth2/v2.0/token"), Scopes = new Dictionary<string, string> { { $"{configuration.ClientId}/contributor offline_access", "API access" } } } } }); return Task.CompletedTask; }); }); return builder; } public static void ConfigureOAuthAuthenticationForScalar(this WebApplication app, AzureAdConfiguration configuration) { if (app.Environment.IsDevelopment()) { app.MapScalarApiReference(options => { options .WithTitle("MyApp API Reference") .WithTheme(ScalarTheme.Solarized) .WithClientButton(false) .WithPreferredScheme(oAuth2Scheme) .WithOAuth2Authentication(oauth => { oauth.ClientId = configuration.SwaggerClientId; oauth.Scopes = [$"{configuration.ClientId}/contributor offline_access"]; }); }); } } } |