-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
95 lines (73 loc) · 2.47 KB
/
Program.cs
File metadata and controls
95 lines (73 loc) · 2.47 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
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using UserFactory.Data;
using UserFactory.Models;
using UserFactory.Services;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
{
Title = "User Management API",
Version = "v1",
Description = @"
### Initial Setup Instructions:
1. **Create Default Admin**
First, execute the endpoint:
🔹 `POST /api/users/init-default-admin`
This will create the default administrator account.
2. **Login as Admin**
Use the default credentials to authenticate:
🔹 Login: `Admin`
🔹 Password: `AdminPass123`
3. **Start Managing Users**
After authentication, you can access all admin endpoints.
### Important Security Notice:
**Authorization cookies are preserved after server restart**
For security reasons, please always:
- Log out explicitly using the `/api/Account/logout` endpoint"
});
});
builder.Services.AddDbContext<WebDbContext>(options =>
options.UseInMemoryDatabase("InMemoryDb"));
builder.Services.AddControllers();
builder.Services.AddTransient<UserService>();
builder.Services.AddTransient<AccountService>();
builder.Services.AddScoped<IPasswordHasher<User>, PasswordHasher<User>>();
builder.Services.AddHttpContextAccessor();
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.Cookie.HttpOnly = true;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
options.Cookie.SameSite = SameSiteMode.Strict;
options.LoginPath = "/api/Account/login";
options.LogoutPath = "/api/Account/logout";
options.ExpireTimeSpan = TimeSpan.FromDays(1);
});
builder.Services.Configure<User>(builder.Configuration.GetSection("DefaultAdminUser"));
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty;
});
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapGet("/", context =>
{
context.Response.Redirect("/swagger");
return Task.CompletedTask;
});
app.MapControllers();
app.Run();