-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
110 lines (90 loc) · 3.38 KB
/
Program.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
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using failure_api.Data;
using failure_api.Models;
using failure_api.Services;
using failure_api.Filters;
using DotNetEnv;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireUppercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequiredLength = 6;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
builder.Services.ConfigureApplicationCookie(options =>
{
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
options.LoginPath = "/api/auth/login"; // Change as needed
options.AccessDeniedPath = "/api/auth/access-denied"; // Change as needed
options.SlidingExpiration = true;
});
Env.Load();
var originUrl = Environment.GetEnvironmentVariable("ORIGIN_URL") ?? "http://localhost:3000";
builder.Services.AddCors(options => {
options.AddPolicy("AllowSpecificOrigins",
builder => builder
.WithOrigins(originUrl)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
// Add application services.
builder.Services.AddScoped<IBadgeService, BadgeService>();
builder.Services.AddScoped<PrivateProfileFilter>();
var app = builder.Build();
app.UseCors("AllowSpecificOrigins");
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
// Init creating admin
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
await CreateRolesAndAdminUser(userManager, roleManager);
}
app.Run();
return;
// Create admin role and user
async Task CreateRolesAndAdminUser(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
{
// checks if the role exists
if (!await roleManager.RoleExistsAsync("Admin"))
{
await roleManager.CreateAsync(new IdentityRole("Admin")); // create the new role
}
// creates new user if it doesn't exist
var adminUser = await userManager.FindByNameAsync(Environment.GetEnvironmentVariable("ADMIN_USERNAME") ?? "admin");
if (adminUser == null)
{
var user = new ApplicationUser
{
UserName = Environment.GetEnvironmentVariable("ADMIN_USERNAME") ?? "admin",
Email = Environment.GetEnvironmentVariable("ADMIN_EMAIL") ?? "[email protected]",
EmailConfirmed = true,
IdGoogle = "admin"
};
var result = await userManager.CreateAsync(user, Environment.GetEnvironmentVariable("ADMIN_PASSWORD") ?? "Admin@1234");
if (result.Succeeded)
{
await userManager.AddToRoleAsync(user, "Admin"); // add the user to the role
}
}
}