在我的
Startup.cs
中,我在 ConfigureServices
中有以下内容:services.AddAuthentication(CertificateAuthenticationDefaults.AuthenticationScheme)
.AddCertificate(options =>
{
options.AllowedCertificateTypes = CertificateTypes.All;
options.Events = new CertificateAuthenticationEvents
{
OnCertificateValidated = context =>
{
// More code to verify certificates
},
OnAuthenticationFailed = context =>
{
// More code
}
};
});
// Other services
而在配置
中:
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
// Endpoints
});
在Program.cs
中,我已经包含了:
webBuilder.ConfigureKestrel(o =>
{
o.ConfigureHttpsDefaults(o =>
o.ClientCertificateMode = ClientCertificateMode.RequireCertificate);
});
如果我在浏览器中连接API,它会提示我选择证书,但是在我选择了证书后,既没有触发
OnCertificateValidated
事件,也没有触发OnAuthenticationFailed
事件。经过进一步测试,我意识到在Startup.cs
中AddCertificate
调用内的整个选项配置委托从未运行。这让我认为我缺少了某种针对Kestrel的配置,但我不知道是什么。请注意,我的Web API不使用IIS托管。还需要做什么来使用基于自签名证书的身份验证?我目前所拥有的代码是基于此处文档中找到的指令: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/certauth?view=aspnetcore-5.0