Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
144 lines (125 loc) · 5.34 KB
/
Copy pathProgram.cs
File metadata and controls
144 lines (125 loc) · 5.34 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
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
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using MPS.API.DAL;
using MPS.API.Shared;
using Newtonsoft.Json;
using System.Text;
using System.Text.Json.Serialization;
namespace MPS.API
{
public class Program
{
public static IConfiguration? Configuration;
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
Configuration = builder.Configuration;
var IssuerSigningKey = Configuration.GetValue<string>("IssuerSigningKey");
if (IssuerSigningKey != null)
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(jwtBearerOptions =>
{
jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters()
{
ValidateActor = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = "Issuer",
ValidAudience = "Audience",
SaveSigninToken = true,
RequireExpirationTime = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(IssuerSigningKey))
};
});
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("System", policy =>
policy.RequireClaim("UserType", UserType.SYS.ToString()));
options.AddPolicy("Organization", policy =>
policy.RequireClaim("UserType", UserType.ORGANIZATION.ToString()));
options.AddPolicy("Provider", policy =>
policy.RequireClaim("UserType", UserType.PROVIDER.ToString()));
});
builder.Services.AddMemoryCache();
// Add services to the container.
builder.Services.AddCors();
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
options.JsonSerializerOptions.WriteIndented = true; // Optional: for better readability
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
{
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = "Bearer"
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
});
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
var ConnectionString = Configuration.GetValue<string>("ConnectionString");
if (ConnectionString != null)
builder.Services.AddDbContext<MpsContext>(options =>
{
options.UseMySQL(ConnectionString);
});
var app = builder.Build();
app.UseExceptionHandler(a => a.Run(async context =>
{
var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
var exception = exceptionHandlerPathFeature?.Error;
var result = JsonConvert.SerializeObject(new
{
IsSuccess = false,
ErrorCode = System.Net.HttpStatusCode.InternalServerError,
Message = exception?.Message,
InnerException = (exception?.InnerException != null) ? exception?.InnerException?.Message : null
});
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(result);
}));
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
options.RoutePrefix = string.Empty;
});
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
}
//app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}