How Can I add Security Definition in Scalar

In Swagger We could Use

builder.Services.AddSwaggerGen(options =>
{
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
options.OperationFilter<SecurityRequirementsOperationFilter>();

builder.Services.AddSwaggerGen(options =>
{
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
options.OperationFilter<SecurityRequirementsOperationFilter>();
for adding tokens and such but How Can I add this functionality in Scalar In their website https://docs.scalar.com/swagger-editor#description/markdown-support they have
security:
- bearerAuth: []
- basicAuth: []
- apiKeyQuery: []
- apiKeyHeader: []
- apiKeyCookie: []
- oAuth2: []
security:
- bearerAuth: []
- basicAuth: []
- apiKeyQuery: []
- apiKeyHeader: []
- apiKeyCookie: []
- oAuth2: []
as the way to add the section to generate the tokens but how to do it in C# code
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference(Options =>
{
Options
.WithTheme(ScalarTheme.BluePlanet)
.WithDefaultHttpClient(ScalarTarget.CSharp, ScalarClient.HttpClient);
});
}
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference(Options =>
{
Options
.WithTheme(ScalarTheme.BluePlanet)
.WithDefaultHttpClient(ScalarTarget.CSharp, ScalarClient.HttpClient);
});
}
Scalar API Documentation — Manage & Create beautiful API Documentat...
With Scalar API Documentation you can create world class documentation your users will love
3 Replies
AbishkarKafle
AbishkarKafleOP3w ago
Fixed
internal sealed class BearerSecuritySchemeTransformer(IAuthenticationSchemeProvider authenticationSchemeProvider) : IOpenApiDocumentTransformer
{
public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken)
{
var authenticationSchemes = await authenticationSchemeProvider.GetAllSchemesAsync();
if (authenticationSchemes.Any(authScheme => authScheme.Name == "Bearer"))
{
var requirements = new Dictionary<string, OpenApiSecurityScheme>
{
["Bearer"] = new OpenApiSecurityScheme
{
Type = SecuritySchemeType.Http,
Scheme = "bearer", // "bearer" refers to the header name here
In = ParameterLocation.Header,
BearerFormat = "Json Web Token"
}
};
document.Components ??= new OpenApiComponents();
document.Components.SecuritySchemes = requirements;
}
}
}
internal sealed class BearerSecuritySchemeTransformer(IAuthenticationSchemeProvider authenticationSchemeProvider) : IOpenApiDocumentTransformer
{
public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken)
{
var authenticationSchemes = await authenticationSchemeProvider.GetAllSchemesAsync();
if (authenticationSchemes.Any(authScheme => authScheme.Name == "Bearer"))
{
var requirements = new Dictionary<string, OpenApiSecurityScheme>
{
["Bearer"] = new OpenApiSecurityScheme
{
Type = SecuritySchemeType.Http,
Scheme = "bearer", // "bearer" refers to the header name here
In = ParameterLocation.Header,
BearerFormat = "Json Web Token"
}
};
document.Components ??= new OpenApiComponents();
document.Components.SecuritySchemes = requirements;
}
}
}
should be used and
builder.Services.AddAuthentication().AddJwtBearer();
builder.Services.AddOpenApi(options =>
{
options.AddDocumentTransformer<BearerSecuritySchemeTransformer>();
});
builder.Services.AddAuthentication().AddJwtBearer();
builder.Services.AddOpenApi(options =>
{
options.AddDocumentTransformer<BearerSecuritySchemeTransformer>();
});
using this authentication and authorization can be used
Micrsoft.AspNetCore.Authentication.JwtBearer
Micrsoft.AspNetCore.Authentication.JwtBearer
package is important that's what I found
AbishkarKafle
AbishkarKafleOP3w ago
without the
builder.Services.AddAuthentication().AddJwtBearer();
builder.Services.AddAuthentication().AddJwtBearer();
No description
AbishkarKafle
AbishkarKafleOP3w ago
builder.Services.AddAuthentication().AddJwtBearer();
builder.Services.AddAuthentication().AddJwtBearer();
with the code enabled
No description
Want results from more Discord servers?
Add your server