Tạo chứng chỉ sử dụng PowerShell với quyền quản trị viên
Quy trình tạo chứng chỉ
Tài liệu tham khảo:
- Tạo và xuất chứng chỉ tự ký trong Windows bằng PowerShell - Cộng đồng phát triển Tencent Cloud
- Cấu hình xác thực chứng chỉ trong ASP.NET Core | Microsoft Learn
- Tạo chứng chỉ tự ký | Microsoft Learn
Câu lệnh tạo chứng chỉ
- Đặt tên chủ thể (tên DNS) là "mylab.wang.io"
- Thời hạn hiệu lực là 10 năm kể từ ngày hiện tại
- Tham số "KeySpec" được đặt thành "KeyExchange" để cho phép trao đổi khóa
- Tham số "KeyExportPolicy" được đặt thành "Exportable" để cho phép xuất khóa riêng
Sử dụng "cert CurrentUser" để tạo cho người dùng hiện tại
Sử dụng "cert LocalMachine" để tạo cho máy tính cục bộ
Dưới đây là ví dụ tạo cho máy cá nhân:
Tên: localhost7029
Chuỗi fingerprint được tạo: 4C8F47849396E076CFC91ABCD33FB9E097724EB6
$certificateParams = @{
DnsName = @("localhost7029", "localhost7029")
CertStoreLocation = "cert:\LocalMachine\My"
NotAfter = (Get-Date).AddYears(20)
FriendlyName = "localhost7029"
KeyUsageProperty = "All"
KeyUsage = @("CertSign", "CRLSign", "DigitalSignature")
}
$newCert = New-SelfSignedCertificate @certificateParams
$passwordSecure = ConvertTo-SecureString -String "1234" -Force -AsPlainText
Get-ChildItem -Path cert:\localMachine\My\4C8F47849396E076CFC91ABCD33FB9E097724EB6 |
Export-PfxCertificate -FilePath C:\git\localhost7029.pfx -Password $passwordSecure
Export-Certificate -Cert cert:\localMachine\My\4C8F47849396E076CFC91ABCD33FB9E097724EB6 -FilePath C:\git\localhost7029.crt
Ví dụ tạo cho người dùng hiện tại:
$certificateParams = @{
DnsName = @("localhost7029", "localhost7029")
CertStoreLocation = "cert:\CurrentUser\My"
NotAfter = (Get-Date).AddYears(20)
FriendlyName = "localhost7029"
KeyUsageProperty = "All"
KeyUsage = @("CertSign", "CRLSign", "DigitalSignature")
}
$newCert = New-SelfSignedCertificate @certificateParams
$passwordSecure = ConvertTo-SecureString -String "1234" -Force -AsPlainText
Get-ChildItem -Path cert:\CurrentUser\My\773575C2221A99FC8FD6DBA737674357C9ED5BDE |
Export-PfxCertificate -FilePath D:\微软练习\RazorPagesMovie\TestCertificate\CurrentUserlocalhost7029.pfx -Password $passwordSecure
Export-Certificate -Cert cert:\CurrentUser\My\773575C2221A99FC8FD6DBA737674357C9ED5BDE -FilePath D:\微软练习\RazorPagesMovie\TestCertificate\CurrentUserlocalhost7029.crt
Kiểm tra thư mục dự án, chứng chỉ đã được xuất vào thư mục dự án.
Nhập vào kho lưu trữ của cơ quan cấp phát chứng chỉ tin cậy
Kiểm tra trạng thái chứng chỉ
Chứng chỉ đã xuất
Lưu ý quan trọng: Sau khi nhập chứng chỉ thành công, cần thiết lập tùy chọn "Luôn sao chép"
Chọn chứng chỉ sau đó có thể xem trên trang web
Dưới đây là cách mở bảng điều khiển chứng chỉ nhanh chóng:
- Mở win+R
- Nhập mmc
- Click "Tệp" → "Thêm/Loại bỏ snap-in" → Chọn "Cá nhân" để xem chứng chỉ vừa tạo localhost7029
Hiện tại chứng chỉ chưa được tin tưởng
Nhập chứng chỉ đã xuất vào kho lưu trữ chứng chỉ gốc đáng tin cậy
Nhập thành công
Kiểm tra trạng thái chứng chỉ
Code mẫu:
Cài đặt NuGet package Microsoft.AspNetCore.Authentication.Certificate
Cấu hình Kestrel bắt buộc phải có chứng chỉ
// Cấu hình dịch vụ KestrelServer
builder.Services.Configure<KestrelServerOptions>(options =>
{
options.ConfigureHttpsDefaults(httpsOptions =>
{
// Bắt buộc phải có chứng chỉ
httpsOptions.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
// Tải chứng chỉ
string certPath = Path.Combine(AppContext.BaseDirectory, "CurrentUserlocalhost7029.pfx");
httpsOptions.ServerCertificate = new X509Certificate2(certPath, "1234");
});
});
// Thêm xác thực chứng chỉ
builder.Services.AddAuthentication(CertificateAuthenticationDefaults.AuthenticationScheme)
.AddCertificate(authOptions =>
{
// Cấu hình loại chứng chỉ được phép là tất cả
authOptions.AllowedCertificateTypes = CertificateTypes.All;
});
Ống dẫn xử lý cần thêm middleware xác thực:
app.UseAuthentication();