-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
151 lines (130 loc) · 6.42 KB
/
Startup.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
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using ProjectFinalEngineer.BusinessManager;
using ProjectFinalEngineer.EntityFramework;
using ProjectFinalEngineer.Models.AggregateRole;
using ProjectFinalEngineer.Models.AggregateUser;
using ProjectFinalEngineer.Services;
using ProjectFinalEngineer.Services.Comment;
namespace ProjectFinalEngineer
{
public class Startup
{
public static string ContentRootPath { get; set; } = null!;
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
ContentRootPath = env.ContentRootPath;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// .NET type Datetime: millisecond, PostgreSQL type timestamp: microsecond => convert timestamp => DatetimeOffset
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
// PositiveInfinity, NegativeInfinity hoặc NaN => DateTime.MaxValue && DateTime.MinValue
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
services.AddDbContext<AppDbContext>(options =>
{
string connectString = Configuration.GetConnectionString("ForumDb");
options.UseNpgsql(connectString);
});
services.AddControllersWithViews();
services.AddRazorPages();
services.AddIdentity<AppUser, IdentityRole>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
// Truy cập IdentityOptions
services.Configure<IdentityOptions>(options =>
{
// Thiết lập về Password
options.Password.RequireDigit = false; // Không bắt phải có số
options.Password.RequireLowercase = false; // Không bắt phải có chữ thường
options.Password.RequireNonAlphanumeric = false; // Không bắt ký tự đặc biệt
options.Password.RequireUppercase = false; // Không bắt buộc chữ in
options.Password.RequiredLength = 3; // Số ký tự tối thiểu của password
options.Password.RequiredUniqueChars = 1; // Số ký tự riêng biệt
// Cấu hình Lockout - khóa user
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5); // Khóa 5 phút
options.Lockout.MaxFailedAccessAttempts = 1000; // Thất bại 1000 lần thì khóa
options.Lockout.AllowedForNewUsers = true;
// Cấu hình về User.
options.User.AllowedUserNameCharacters = // các ký tự đặt tên user
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
options.User.RequireUniqueEmail = true; // Email là duy nhất
// Cấu hình đăng nhập.
options.SignIn.RequireConfirmedEmail = true; // Cấu hình xác thực địa chỉ email (email phải tồn tại)
options.SignIn.RequireConfirmedPhoneNumber = false; // Xác thực số điện thoại
options.SignIn.RequireConfirmedAccount = true;
});
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/login/";
options.LogoutPath = "/logout/";
options.AccessDeniedPath = "/khongduoctruycap.html";
});
services.AddAutoMapper(typeof(Startup));
services.AddAuthentication()
.AddGoogle(options =>
{
var gconfig = Configuration.GetSection("Authentication:Google");
options.ClientId = gconfig["ClientId"];
options.ClientSecret = gconfig["ClientSecret"];
// https://localhost:5001/signin-google
options.CallbackPath = "/dang-nhap-tu-google";
})
.AddFacebook(options =>
{
var fconfig = Configuration.GetSection("Authentication:Facebook");
options.AppId = fconfig["AppId"];
options.AppSecret = fconfig["AppSecret"];
options.CallbackPath = "/dang-nhap-tu-facebook";
})
// .AddTwitter()
// .AddMicrosoftAccount()
;
services.AddOptions();
var mailsetting = Configuration.GetSection("MailSettings");
services.Configure<MailSettings>(mailsetting);
services.AddSingleton<IEmailSender, SendMailService>();
services.AddTransient<ICommentBusinessManager, CommentBusinessManager>();
services.AddTransient<ICommentService, CommentService>();
services.AddSingleton<IdentityErrorDescriber, App.Services.AppIdentityErrorDescriber>();
services.AddAuthorization(options =>
{
options.AddPolicy("ViewManageMenu", builder =>
{
builder.RequireAuthenticatedUser();
builder.RequireRole(RoleName.Administrator);
});
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
// URL: /{controller}/{action}/{id?}
// Home/Home
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Home}/{id?}");
endpoints.MapRazorPages();
});
}
}
}