How would you implement mutual TLS (mTLS) authentication in ASP.NET Core, where the server also validates a client-presented certificate?
Short answer
Mutual TLS requires configuring Kestrel to request and validate a client certificate during the TLS handshake (ClientCertificateMode.RequireCertificate), then using the Certificate Authentication middleware (AddCertificate()) to translate the validated client certificate into a…
Advertisement
AModel answer
Mutual TLS requires configuring Kestrel to request and validate a client certificate during the TLS handshake (ClientCertificateMode.RequireCertificate), then using the Certificate Authentication middleware (AddCertificate()) to translate the validated client certificate into a ClaimsPrincipal usable by the standard authentication/authorization system -- commonly used for service-to-service authentication in zero-trust architectures or B2B API integrations where both sides need cryptographic proof of identity, not just the server.
builder.WebHost.ConfigureKestrel(options => {
options.ConfigureHttpsDefaults(o => o.ClientCertificateMode = ClientCertificateMode.RequireCertificate);
});
builder.Services.AddAuthentication(CertificateAuthenticationDefaults.AuthenticationScheme)
.AddCertificate(options => {
options.AllowedCertificateTypes = CertificateTypes.Chained;
options.Events = new CertificateAuthenticationEvents {
OnCertificateValidated = context => {
// custom validation, e.g. checking against an allowlist of thumbprints
context.Success();
return Task.CompletedTask;
}
};
});Real-world example
A B2B payment processing API requires partner organizations to present a client certificate issued specifically for their integration, cryptographically verifying the calling party's identity beyond just an API key, appropriate for a high-security financial integration.
Likely follow-ups
- How does mTLS certificate validation and revocation checking work in practice?
- What's the operational complexity of distributing and rotating client certificates to multiple partner organizations?
Related topics