ASP.NET Core 中的开发证书身份验证问题
Problem with Development Certificate authentication in ASP.NET Core
我正在使用 .NET Core 3.1 和 Microsoft.AspNetCore.Authentication.Certificate 包开发 Web API。
这是我的配置方法:
public void Configure(IApplicationBuilder app) {
//if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
AutofacContainer = app.ApplicationServices.GetAutofacRoot();
app.UseRouting();
app.UseCertificateForwarding();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
});
}
我设置 services.AddAuthentication 如下:
services.AddAuthentication(
CertificateAuthenticationDefaults.AuthenticationScheme)
.AddCertificate(options => {
options.Events = new CertificateAuthenticationEvents {
OnCertificateValidated = context => {
var claims = new[]
{
new Claim(
ClaimTypes.NameIdentifier,
context.ClientCertificate.Subject,
ClaimValueTypes.String,
context.Options.ClaimsIssuer),
new Claim(ClaimTypes.Name,
context.ClientCertificate.Subject,
ClaimValueTypes.String,
context.Options.ClaimsIssuer)
};
context.Principal = new ClaimsPrincipal(
new ClaimsIdentity(claims, context.Scheme.Name));
context.Success();
return Task.CompletedTask;
},
OnAuthenticationFailed = context => {
context.Fail("invalid cert");
return Task.CompletedTask;
}
};
});
在applicationHost.config中,这是我的元素:
<access sslFlags="Ssl, SslNegotiateCert, SslRequireCert, Ssl128" />
因此,我的应用程序需要 SSL。
有了我的智能卡上的证书,OnCertificateValidated 就会毫无问题地触发。对于根测试证书,我预计 OnAuthenticationFailed 事件会触发,因为 options.RevocationMode 的默认值为 X509RevocationMode.Online。然而,有了这个测试证书,两个事件都不会触发。
当我将 options.RevocationMode 设置为 X509RevocationMode.NoCheck 时,OnCertificateValidated 事件实际上会触发测试证书;这意味着在它失败之前。如果是这种情况,那么为什么 OnAuthenticationFailed 事件没有触发。
更新
我下载了符号并逐步完成了 CertificateAuthenticationHandler.cs 中的代码。证书验证失败,代码运行
return AuthenticateResult.Fail("Client certificate failed validation.");
但是 OnAuthenticationFailed 的处理程序没有触发。
感谢任何人提供的帮助。
长话短说,该行为是设计使然。
如我的更新所述,代码会在证书正确“验证”时触发 AuthenticateResult.Fail;它不会触发 Events.AuthenticationFailed.
我在 GitHub 上提出了一个问题,他们将在下一个版本中更新代码。
我正在使用 .NET Core 3.1 和 Microsoft.AspNetCore.Authentication.Certificate 包开发 Web API。
这是我的配置方法:
public void Configure(IApplicationBuilder app) {
//if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
AutofacContainer = app.ApplicationServices.GetAutofacRoot();
app.UseRouting();
app.UseCertificateForwarding();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
});
}
我设置 services.AddAuthentication 如下:
services.AddAuthentication(
CertificateAuthenticationDefaults.AuthenticationScheme)
.AddCertificate(options => {
options.Events = new CertificateAuthenticationEvents {
OnCertificateValidated = context => {
var claims = new[]
{
new Claim(
ClaimTypes.NameIdentifier,
context.ClientCertificate.Subject,
ClaimValueTypes.String,
context.Options.ClaimsIssuer),
new Claim(ClaimTypes.Name,
context.ClientCertificate.Subject,
ClaimValueTypes.String,
context.Options.ClaimsIssuer)
};
context.Principal = new ClaimsPrincipal(
new ClaimsIdentity(claims, context.Scheme.Name));
context.Success();
return Task.CompletedTask;
},
OnAuthenticationFailed = context => {
context.Fail("invalid cert");
return Task.CompletedTask;
}
};
});
在applicationHost.config中,这是我的元素:
<access sslFlags="Ssl, SslNegotiateCert, SslRequireCert, Ssl128" />
因此,我的应用程序需要 SSL。
有了我的智能卡上的证书,OnCertificateValidated 就会毫无问题地触发。对于根测试证书,我预计 OnAuthenticationFailed 事件会触发,因为 options.RevocationMode 的默认值为 X509RevocationMode.Online。然而,有了这个测试证书,两个事件都不会触发。
当我将 options.RevocationMode 设置为 X509RevocationMode.NoCheck 时,OnCertificateValidated 事件实际上会触发测试证书;这意味着在它失败之前。如果是这种情况,那么为什么 OnAuthenticationFailed 事件没有触发。
更新
我下载了符号并逐步完成了 CertificateAuthenticationHandler.cs 中的代码。证书验证失败,代码运行
return AuthenticateResult.Fail("Client certificate failed validation.");
但是 OnAuthenticationFailed 的处理程序没有触发。
感谢任何人提供的帮助。
长话短说,该行为是设计使然。
如我的更新所述,代码会在证书正确“验证”时触发 AuthenticateResult.Fail;它不会触发 Events.AuthenticationFailed.
我在 GitHub 上提出了一个问题,他们将在下一个版本中更新代码。