This repository was archived by the owner on Mar 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
256 lines (217 loc) · 8.6 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using RealEstateApp.Api.Auth;
using RealEstateApp.Api.DatabaseContext;
using RealEstateApp.Api.Entity;
using RealEstateApp.Api.Middleware;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
ConfigurationManager configuration = builder.Configuration;
#region Data Access
#region Real Estate
// Use SqlServer
builder.Services.AddDbContext<RealEstateContext>(options => options.UseNpgsql(configuration.GetConnectionString("RealEstate")));
#endregion
#region Real Estate Identity
builder.Services.AddDbContext<RealEstateIdentityContext>(options => options.UseNpgsql(configuration.GetConnectionString("RealEstateIdentity")));
#endregion
#endregion
#region Authentication and Authorization
// For Identity
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<RealEstateIdentityContext>()
.AddDefaultTokenProviders();
// Adding Authentication
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
// Adding Jwt Bearer
.AddJwtBearer(options =>
{
options.SaveToken = true;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidAudience = configuration["JWT:ValidAudience"],
ValidIssuer = configuration["JWT:ValidIssuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["JWT:Secret"]))
};
});
#endregion
#region Controllers and Endpoints
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(swagger =>
{
//This is to generate the Default UI of Swagger Documentation
swagger.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Web API",
Description = "ASP.NET Core 6 Web API"
});
// To Enable authorization using Swagger (JWT)
swagger.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
{
Name = "Authorization",
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"",
});
swagger.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
});
#endregion
#region Build App and Run
var app = builder.Build();
var logger = app.Logger;
#region Apply migrations and seeding on boot
var migrateOnBoot = configuration["ApplyMigrationsOnBoot"] == "Y";
if (!migrateOnBoot) logger.LogInformation("ApplyMigrationsOnBoot not specified, skipping migration checks and seeding.\nTo apply migration checks and seeding on boot, set \"ApplyMigrationsOnBoot\" to \"Y\" in appsettings.json.");
else
{
using var scope = app.Services.CreateScope();
var realEstateIdentityContext = scope.ServiceProvider.GetRequiredService<RealEstateIdentityContext>();
var realEstateContext = scope.ServiceProvider.GetRequiredService<RealEstateContext>();
logger.LogInformation("Checking for pending migrations...");
var pendingMigrations = realEstateIdentityContext.Database.GetPendingMigrations().Any();
if (!pendingMigrations)
{
logger.LogInformation("No pending migrations.");
}
else
{
logger.LogInformation("Migrating database...");
await realEstateContext.Database.MigrateAsync();
logger.LogInformation("Migration complete.");
}
logger.LogInformation("Checking for pending authentication migrations...");
var pendingAuthMigrations = realEstateIdentityContext.Database.GetPendingMigrations().Any();
if (!pendingAuthMigrations)
{
logger.LogInformation("No pending authentication migrations.");
}
else
{
logger.LogInformation("Migrating authentication database...");
await realEstateIdentityContext.Database.MigrateAsync();
logger.LogInformation("Auth migration complete.");
}
// Seed database
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<IdentityUser>>();
logger.LogInformation("Checking for admin account...");
var adminUser = await userManager.FindByNameAsync("admin");
if (adminUser != null)
{
logger.LogInformation("Admin account found, skipping authentication seeding.");
}
else
{
logger.LogInformation("Seeding authentication database...");
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var adminRole = await roleManager.FindByNameAsync(UserRoles.Admin);
if (adminRole == null) await roleManager.CreateAsync(new IdentityRole(UserRoles.Admin));
var userRole = await roleManager.FindByNameAsync(UserRoles.User);
if (userRole == null) await roleManager.CreateAsync(new IdentityRole(UserRoles.User));
var newAdminUser = new IdentityUser()
{
UserName = "admin",
SecurityStamp = Guid.NewGuid().ToString(),
Email = configuration["Credentials:AdminEmail"]
};
var result = await userManager.CreateAsync(newAdminUser, configuration["Credentials:AdminPassword"]);
if (result.Succeeded)
{
await userManager.AddToRoleAsync(newAdminUser, UserRoles.User);
await userManager.AddToRoleAsync(newAdminUser, UserRoles.Admin);
var newUser = new User()
{
Name = newAdminUser.UserName,
Email = newAdminUser.Email,
Username = newAdminUser.UserName
};
realEstateContext.Users.Add(newUser);
await realEstateContext.SaveChangesAsync();
}
else
{
logger.LogInformation("Error creating admin user:");
foreach (var error in result.Errors) logger.LogError(error.Description);
}
logger.LogInformation("Authentication seeding complete.");
}
// Add default property types, statuses, and currencies
logger.LogInformation("Checking for default currencies, property types, and statuses...");
var currencyExists = await realEstateContext.Currencies.AsNoTracking().AnyAsync();
if (!currencyExists)
{
await realEstateContext.Currencies.AddRangeAsync(Currency.GenerateDefault());
await realEstateContext.SaveChangesAsync();
logger.LogInformation("Default currencies added.");
}
else logger.LogInformation("Default currencies already exist.");
var typeExists = await realEstateContext.PropertyTypes.AsNoTracking().AnyAsync();
if (!typeExists)
{
await realEstateContext.PropertyTypes.AddRangeAsync(PropertyType.GenerateDefault());
await realEstateContext.SaveChangesAsync();
logger.LogInformation("Default property types added.");
}
else logger.LogInformation("Default property types already exist.");
var statusExists = await realEstateContext.PropertyStatuses.AsNoTracking().AnyAsync();
if (!statusExists)
{
await realEstateContext.PropertyStatuses.AddRangeAsync(PropertyStatus.GenerateDefault());
await realEstateContext.SaveChangesAsync();
logger.LogInformation("Default property statuses added.");
}
else logger.LogInformation("Default property statuses already exist.");
logger.LogInformation("Seeding process complete.");
}
#endregion
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// Middleware
app.UseTraceMiddleware();
app.UsePerformanceMiddleware();
app.UseExceptionHandlerMiddleware();
// app.UseLoggingMiddleware();
// Cors
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
);
// Authentication & Authorization
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
#endregion