-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
116 lines (100 loc) · 3.29 KB
/
Copy pathProgram.cs
File metadata and controls
116 lines (100 loc) · 3.29 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
using Baratrum.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi;
using Baratrum.Services;
using Baratrum.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Name = "Authorization",
Type = SecuritySchemeType.Http,
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "Enter your valid token in the text input below.\r\n\r\nExample: 'eyJhbGci...'"
});
// Use a delegate here to satisfy the OpenApiDocument requirRheement
options.AddSecurityRequirement(document => new OpenApiSecurityRequirement
{
{
new OpenApiSecuritySchemeReference("Bearer", document),
new List<string>()
}
});
});
// Configure the MSSQL Database connection
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddIdentityApiEndpoints<ApplicationUser>(options => {
options.SignIn.RequireConfirmedEmail = !METADATA.RUNNINGLOCAL;
})
.AddEntityFrameworkStores<AppDbContext>()
.AddUserManager<CustomUserManager>();
builder.Services.AddAuthentication();
builder.Services.AddAuthorization();
builder.Services.AddTransient<IEmailSender, EmailSender>();
builder.Services.AddCors(options =>
{
options.AddPolicy("BaratrumPolicy", policy =>
{
if (builder.Environment.IsDevelopment() || METADATA.RUNNINGLOCAL)
{
policy.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
}
else
{
// Strict policy for production
policy.WithOrigins("https://baratrum.com", "https://www.baratrum.com","localapp://localhost")
.AllowAnyHeader()
.AllowAnyMethod();
}
});
});
builder.Services.AddHttpClient();
builder.Services.AddControllers();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.Database.Migrate();
}
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors("BaratrumPolicy");
// Add Authentication and Authorization middleware
app.UseWebSockets();
app.Use(async (context, next) =>
{
var request = context.Request;
if (request.Path.StartsWithSegments("/ws") && request.Query.TryGetValue("access_token", out var token))
{
request.Headers.Authorization = $"Bearer {token}";
}
await next();
});
app.UseAuthentication();
app.UseAuthorization();
app.MapIdentityApi<ApplicationUser>()
.AddEndpointFilter(async (context, next) =>
{
if ((context.HttpContext.Request.Path.Equals("/register", StringComparison.OrdinalIgnoreCase) ||
context.HttpContext.Request.Path.Equals("/confirmEmail", StringComparison.OrdinalIgnoreCase)) &&
!METADATA.RUNNINGLOCAL )
{
return Results.NotFound("");
}
return await next(context);
});
app.MapControllers();
app.Run();