Advertisement
Intermediate .NET MAUI, Security & Authentication

How do you enforce SSL/certificate pinning in MAUI?

Short answer

Validate the server certificate in the HttpClient handler by comparing its public key/thumbprint to a pinned value, rejecting mismatches. Configure via HttpClientHandler.ServerCertificateCustomValidationCallback (or platform handlers).

Save .NET MAUI mock interview
Advertisement
AModel answer
Validate the server certificate in the HttpClient handler by comparing its public key/thumbprint to a pinned value, rejecting mismatches. Configure via HttpClientHandler.ServerCertificateCustomValidationCallback (or platform handlers). Pinning defends against MITM but requires a rotation plan when certs change.
var handler = new HttpClientHandler
{
    ServerCertificateCustomValidationCallback = (m, cert, chain, e) =>
        cert!.GetCertHashString() == PinnedThumbprint
};
Real-world example
A finance app pins its API certificate so a compromised network CA can't intercept traffic.
Likely follow-ups
  • What's the downside of pinning? | Where do you plan for cert rotation?
Related topics