|
| 1 | +namespace Simple.TestWebApi; |
| 2 | + |
1 | 3 | using Microsoft.AspNetCore.Authentication.JwtBearer; |
2 | 4 | using Microsoft.AspNetCore.Builder; |
3 | 5 | using Microsoft.AspNetCore.Hosting; |
|
7 | 9 | using Microsoft.OpenApi; |
8 | 10 | using System.Text; |
9 | 11 |
|
10 | | -namespace Simple.TestWebApi |
| 12 | +public class Startup |
11 | 13 | { |
12 | | - public class Startup |
| 14 | + public Startup(IConfiguration configuration) |
13 | 15 | { |
14 | | - public Startup(IConfiguration configuration) |
15 | | - { |
16 | | - Configuration = configuration; |
17 | | - } |
| 16 | + Configuration = configuration; |
| 17 | + } |
18 | 18 |
|
19 | | - public IConfiguration Configuration { get; } |
| 19 | + public IConfiguration Configuration { get; } |
20 | 20 |
|
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 => |
52 | 29 | { |
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 => |
66 | 34 | { |
67 | | - OpenApiSecuritySchemeReference schemeRef = new("Bearer"); |
68 | | - OpenApiSecurityRequirement requirement = new() |
| 35 | + x.RequireHttpsMetadata = false; |
| 36 | + x.SaveToken = true; |
| 37 | + x.TokenValidationParameters = new TokenValidationParameters |
69 | 38 | { |
70 | | - [schemeRef] = [] |
| 39 | + ValidateIssuerSigningKey = true, |
| 40 | + IssuerSigningKey = new SymmetricSecurityKey(key), |
| 41 | + ValidateIssuer = false, |
| 42 | + ValidateAudience = false |
71 | 43 | }; |
72 | | - return requirement; |
73 | 44 | }); |
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 => |
81 | 47 | { |
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; |
86 | 49 |
|
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 | + }; |
89 | 60 |
|
90 | | - app.UseAuthentication(); |
91 | | - app.UseAuthorization(); |
| 61 | + // 2. Registramos a definição usando o ID |
| 62 | + c.AddSecurityDefinition(schemeId, jwtSecurityScheme); |
92 | 63 |
|
93 | | - app.UseEndpoints(endpoints => |
| 64 | + // 3. Criamos o requisito usando a nova classe de referência |
| 65 | + c.AddSecurityRequirement(document => |
94 | 66 | { |
95 | | - endpoints.MapControllers(); |
| 67 | + OpenApiSecuritySchemeReference schemeRef = new("Bearer"); |
| 68 | + OpenApiSecurityRequirement requirement = new() |
| 69 | + { |
| 70 | + [schemeRef] = [] |
| 71 | + }; |
| 72 | + return requirement; |
96 | 73 | }); |
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 | + }); |
98 | 97 | } |
99 | 98 | } |
0 commit comments