/
Startup.cs
220 lines (188 loc) · 7.55 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using DemoMiddleware.Extentsions;
using DemoMiddleware.IServices;
using DemoMiddleware.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Swashbuckle.AspNetCore.Swagger;
namespace DemoMiddleware
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add Iservice
services.AddTransient<ILoginService, LoginService>();
// Add JWToken
//Add authen fixbug cannot get Claims
services.AddAuthentication(o =>
{
o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(cfg =>
{
cfg.RequireHttpsMetadata = false;
cfg.SaveToken = true;
cfg.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = Configuration["Tokens:Issuer"],
ValidAudience = Configuration["Tokens:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Tokens:Key"]))
};
});
// Add Swagger
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "API Demo",
// Version = "1.0",
//Description = "This API Customer",
//Contact = new OpenApiContact
//{
// Name = "DamNgocSon",
// Email = "damngocsonIT@gmail.com",
// Url = new Uri("https://sonlanggtu.github.io/"),
//}
});
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please insert JWT with Bearer into field",
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
var result = new OpenApiSecurityRequirement();
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header,
},
new List<string>()
}
});
});
// Add Controller
services.AddControllersWithViews();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseExceptionHandler(options =>
{
options.Run(async context =>
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(">>>>> UseExceptionHandler");
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
var ex = context.Features.Get<IExceptionHandlerFeature>()?.Error;
if (ex == null) return;
var error = new
{
message = ex.Message
};
context.Response.ContentType = "application/json";
context.Response.Headers.Add("Access-Control-Allow-Credentials", new[] { "true" });
context.Response.Headers.Add("Access-Control-Allow-Origin", new[] { Configuration["AllowedHosts"] });
using (var writer = new StreamWriter(context.Response.Body))
{
new JsonSerializer().Serialize(writer, error);
await writer.FlushAsync().ConfigureAwait(false);
}
});
});
// Use Swagger
app.UseSwagger(c =>
{
c.PreSerializeFilters.Add((document, request) =>
{
var paths = document.Paths.ToDictionary(item => item.Key.ToLowerInvariant(), item => item.Value);
document.Paths.Clear();
foreach (var pathItem in paths)
{
document.Paths.Add(pathItem.Key, pathItem.Value);
}
});
});
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAPI Core & Dapper V1");
});
// check Environment
if (env.IsDevelopment())
{
// app.UseDeveloperExceptionPage();
}
else
{
// app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
// app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
// Customize MiddleWare
//app.Use(async (res, next) =>
//{
// await res.Response.WriteAsync("Middleware 1 => ");
// await next.Invoke();
// await res.Response.WriteAsync("Edit Middleware 1 => ");
//});
//app.Use(async (res, next) =>
//{
// await res.Response.WriteAsync("Middleware 2 =>");
// await next.Invoke();
// await res.Response.WriteAsync("Edit Middleware 2 =>");
//});
//// app.UseMiddleware<InjectMiddleware>();
//// use extentionMethod
//app.UseMiddlewareExtention();
//app.Run(async (res) =>
//{
// await res.Response.WriteAsync("Middleware 4 =>");
//});
app.UseMiddleware<JwtMiddleware>();
app.UseEndpoints(endpoints =>
{
// endpoints.MapControllers();
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}