Skip to content

Commit aec18be

Browse files
Update startup for new openapi
1 parent fa16013 commit aec18be

1 file changed

Lines changed: 72 additions & 73 deletions

File tree

Simple.TestWebApi/Startup.cs

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
namespace Simple.TestWebApi;
2+
13
using Microsoft.AspNetCore.Authentication.JwtBearer;
24
using Microsoft.AspNetCore.Builder;
35
using Microsoft.AspNetCore.Hosting;
@@ -7,93 +9,90 @@
79
using Microsoft.OpenApi;
810
using System.Text;
911

10-
namespace Simple.TestWebApi
12+
public class Startup
1113
{
12-
public class Startup
14+
public Startup(IConfiguration configuration)
1315
{
14-
public Startup(IConfiguration configuration)
15-
{
16-
Configuration = configuration;
17-
}
16+
Configuration = configuration;
17+
}
1818

19-
public IConfiguration Configuration { get; }
19+
public IConfiguration Configuration { get; }
2020

21-
// This method gets called by the runtime. Use this method to add services to the container.
22-
public void ConfigureServices(IServiceCollection services)
23-
{
24-
// API stuff
25-
services.AddControllers();
26-
// Auth stuff
27-
var key = Encoding.ASCII.GetBytes(Auth.Token.Secret);
28-
services.AddAuthentication(x =>
29-
{
30-
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
31-
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
32-
})
33-
.AddJwtBearer(x =>
34-
{
35-
x.RequireHttpsMetadata = false;
36-
x.SaveToken = true;
37-
x.TokenValidationParameters = new TokenValidationParameters
38-
{
39-
ValidateIssuerSigningKey = true,
40-
IssuerSigningKey = new SymmetricSecurityKey(key),
41-
ValidateIssuer = false,
42-
ValidateAudience = false
43-
};
44-
});
45-
// Swagger stuff
46-
services.AddSwaggerGen(c =>
47-
{
48-
var schemeId = JwtBearerDefaults.AuthenticationScheme;
49-
50-
// 1. Criamos o esquema SEM a propriedade Reference (que não existe mais)
51-
var jwtSecurityScheme = new OpenApiSecurityScheme
21+
// This method gets called by the runtime. Use this method to add services to the container.
22+
public void ConfigureServices(IServiceCollection services)
23+
{
24+
// API stuff
25+
services.AddControllers();
26+
// Auth stuff
27+
var key = Encoding.ASCII.GetBytes(Auth.Token.Secret);
28+
services.AddAuthentication(x =>
5229
{
53-
Scheme = "bearer",
54-
BearerFormat = "JWT",
55-
Name = "Authorization",
56-
In = ParameterLocation.Header,
57-
Type = SecuritySchemeType.Http,
58-
Description = "Put **_ONLY_** your JWT Bearer token on textbox below!"
59-
};
60-
61-
// 2. Registramos a definição usando o ID
62-
c.AddSecurityDefinition(schemeId, jwtSecurityScheme);
63-
64-
// 3. Criamos o requisito usando a nova classe de referência
65-
c.AddSecurityRequirement(document =>
30+
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
31+
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
32+
})
33+
.AddJwtBearer(x =>
6634
{
67-
OpenApiSecuritySchemeReference schemeRef = new("Bearer");
68-
OpenApiSecurityRequirement requirement = new()
35+
x.RequireHttpsMetadata = false;
36+
x.SaveToken = true;
37+
x.TokenValidationParameters = new TokenValidationParameters
6938
{
70-
[schemeRef] = []
39+
ValidateIssuerSigningKey = true,
40+
IssuerSigningKey = new SymmetricSecurityKey(key),
41+
ValidateIssuer = false,
42+
ValidateAudience = false
7143
};
72-
return requirement;
7344
});
74-
75-
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Simple.TestWebApi", Version = "v1" });
76-
});
77-
}
78-
79-
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
80-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
45+
// Swagger stuff
46+
services.AddSwaggerGen(c =>
8147
{
82-
// aways use swagger, this is a test app....
83-
app.UseDeveloperExceptionPage();
84-
app.UseSwagger();
85-
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Simple.TestWebApi v1"));
48+
var schemeId = JwtBearerDefaults.AuthenticationScheme;
8649

87-
// normal stuff
88-
app.UseRouting();
50+
// 1. Criamos o esquema SEM a propriedade Reference (que não existe mais)
51+
var jwtSecurityScheme = new OpenApiSecurityScheme
52+
{
53+
Scheme = "bearer",
54+
BearerFormat = "JWT",
55+
Name = "Authorization",
56+
In = ParameterLocation.Header,
57+
Type = SecuritySchemeType.Http,
58+
Description = "Put **_ONLY_** your JWT Bearer token on textbox below!"
59+
};
8960

90-
app.UseAuthentication();
91-
app.UseAuthorization();
61+
// 2. Registramos a definição usando o ID
62+
c.AddSecurityDefinition(schemeId, jwtSecurityScheme);
9263

93-
app.UseEndpoints(endpoints =>
64+
// 3. Criamos o requisito usando a nova classe de referência
65+
c.AddSecurityRequirement(document =>
9466
{
95-
endpoints.MapControllers();
67+
OpenApiSecuritySchemeReference schemeRef = new("Bearer");
68+
OpenApiSecurityRequirement requirement = new()
69+
{
70+
[schemeRef] = []
71+
};
72+
return requirement;
9673
});
97-
}
74+
75+
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Simple.TestWebApi", Version = "v1" });
76+
});
77+
}
78+
79+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
80+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
81+
{
82+
// aways use swagger, this is a test app....
83+
app.UseDeveloperExceptionPage();
84+
app.UseSwagger();
85+
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Simple.TestWebApi v1"));
86+
87+
// normal stuff
88+
app.UseRouting();
89+
90+
app.UseAuthentication();
91+
app.UseAuthorization();
92+
93+
app.UseEndpoints(endpoints =>
94+
{
95+
endpoints.MapControllers();
96+
});
9897
}
9998
}

0 commit comments

Comments
 (0)