dotnet / dotnet/aspnetcore

[Spec] WebApi Project Template Add the ability to generate boiler plate code for authorization and Bearer Token

Open
#34,560 2 comments 0 reactions 0 assignees View on GitHub
area-auth feature-templates Tooling
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

Most apis cannot do without this day and age but to add a Bearer token and swagger documentation to the structure of their web api.

I think an option to be added to the new project screen for webapi. It seems very mundan code having to recreate it each time in a web api project and would create more continuity for the api to be standard.

***b*File New WIndowb***
Project Type: Web Api

Checkbox for Swagger Docs
Checkbox for Bearer Tokens

Upon chosen this option the system should be smart enough to add the code to our header calls for httpclient

It should add the following code as default to the configure section.

services.AddSwaggerGen(c => {
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Cella Crm", Version = "v1" });
// c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
//Expose XML comments in doc.
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description =
"JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header
},
new List()
}
});
});

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => {
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration.GetSection(Constants.ApiSecretValue).Value)),
ValidateIssuer = false,
ValidateAudience = false
};
});

Also as I am implementing swagger we should configure it as well in the configure section.

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
}
I understand that Microsoft are re doing web apis at present maybe this could all be replaced with a simple call.

webpi->setupbarrerToken()

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.