Cần cài đặt các NuGet packages sau:
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.AspNetCore.Identity.UI
MailKit (Nếu muốn xác thực bằng email)
Microsoft.AspNetCore.Authentication.Facebook (Nếu muốn xác thực bằng facebook)
Microsoft.AspNetCore.Authentication.Google (Nếu muốn xác thực bằng google)
// Thay đổi kế thừa từ DbContext sang IdentityDbContext<AppUser>
public class TechStoreContext : IdentityDbContext<AppUser>
{
public TechStoreContext(DbContextOptions<TechStoreContext> options)
: base(options)
{
}
// Gọi phương thức cơ sở của Identity trong OnModelCreating
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); // Quan trọng! Phải gọi phương thức này
// Cấu hình các entity khác của bạn ở đây
}
}Chạy lệnh sau trong Terminal:
dotnet ef migrations add InitIdentityNếu bạn đang sử dụng Database First và gặp các bảng trùng lặp, cần xóa các bảng cũ (nếu có).
Chạy lệnh sau để áp dụng các thay đổi lên database:
dotnet ef database updatebuilder.Services.AddIdentity<AppUser, IdentityRole>(options =>
{
options.SignIn.RequireConfirmedAccount = true;
options.SignIn.RequireConfirmedEmail = true;
})
.AddDefaultUI()
.AddEntityFrameworkStores<TechStoreContext>()
.AddDefaultTokenProviders();builder.Services.AddAuthentication()
.AddGoogle(googleOptions =>
{
googleOptions.ClientId = builder.Configuration["Authentication:Google:ClientId"]!;
googleOptions.ClientSecret = builder.Configuration["Authentication:Google:ClientSecret"]!;
})
.AddFacebook(facebookOptions =>
{
facebookOptions.AppId = builder.Configuration["Authentication:Facebook:AppId"]!;
facebookOptions.AppSecret = builder.Configuration["Authentication:Facebook:AppSecret"]!;
});app.UseAuthentication();
app.UseAuthorization();{
"Authentication": {
"Google": {
"ClientId": "your-google-client-id",
"ClientSecret": "your-google-client-secret"
},
"Facebook": {
"AppId": "your-facebook-app-id",
"AppSecret": "your-facebook-app-secret"
}
}
}{
"EmailSettings": {
"SmtpServer": "smtp.gmail.com",
"SmtpPort": 587,
"FromName": "YourAppName",
"FromEmail": "your-email@gmail.com",
"Password": "your-app-password"
}
}builder.Services.Configure<EmailSettings>(builder.Configuration.GetSection("EmailSettings"));
builder.Services.AddTransient<IEmailSender, EmailSenderService>();options.SignIn.RequireConfirmedEmail = true;options.SignIn.RequireConfirmedAccount = true;- 🔗 Đảm bảo đã cấu hình đúng connection string cho database
- 🔄 Thực hiện migration cho Identity tables
- 🛡️ Cấu hình và kiểm tra kỹ các thiết lập bảo mật
- ✅ Test kỹ chức năng xác thực và đăng nhập trước khi deploy
- 🔒 Đảm bảo sử dụng HTTPS cho các endpoints liên quan đến authentication
- 📦 Đã cài đặt đầy đủ các packages
- 🗃️ Đã sửa DbContext để kế thừa IdentityDbContext
- 📝 Đã gọi base.OnModelCreating trong DbContext
- 🔄 Đã tạo migration cho Identity
-
⚠️ Đã xử lý các bảng trùng lặp (nếu có) - 📤 Đã update database thành công
- ⚙️ Đã cấu hình Identity trong Program.cs
- 🔑 Đã thiết lập các keys cho authentication providers
- 📧 Đã cấu hình email service
- 🔒 Đã kiểm tra các vấn đề bảo mật
- ✅ Đã test toàn bộ quy trình đăng ký/đăng nhập
- 🛡️ Đã bật HTTPS
Last updated: 2025-04-22 14:02:38 UTC by @Vuong1411