-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
80 lines (66 loc) · 2.26 KB
/
Copy pathProgram.cs
File metadata and controls
80 lines (66 loc) · 2.26 KB
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
using Gateway.Api.Handlers;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using MMLib.SwaggerForOcelot.DependencyInjection;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true)
.AddJsonFile("swagger.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
var jwtSettings = builder.Configuration.GetSection("JwtSettings");
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwtSettings["Issuer"],
ValidAudience = jwtSettings["Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings["Key"]))
};
});
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowConfigured", policy =>
{
var origins = builder.Configuration.GetSection("cors:origins")
.GetChildren()
.Select(c => c.GetValue<string>("origin"))
.Where(o => !string.IsNullOrEmpty(o))
.ToArray();
policy.WithOrigins(origins)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
builder.Services.AddHttpClient()
.ConfigureHttpClient((sp, client) =>
{
client.DefaultRequestHeaders.Add("User-Agent", "API-Gateway/1.0");
})
.AddHttpMessageHandler<BlacklistHandler>();
builder.Services.AddScoped<BlacklistHandler>();
builder.Services
.AddOcelot()
.AddConsul();
builder.Services.AddSwaggerForOcelot(builder.Configuration);
var app = builder.Build();
app.UseSwaggerForOcelotUI(opt =>
{
opt.PathToSwaggerGenerator = "/swagger/docs";
});
app.UseHttpsRedirection();
app.UseCors("AllowConfigured");
app.UseAuthentication();
app.UseAuthorization();
await app.UseOcelot();
app.Run();