Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
we are getting 403 in core webapi using certificate pfx


we have hosted dotnet core api in iis. we are using certificate authentication
in post method. we are getting 403 in postman.

What I have tried:

<pre>we are getting 403 in core webapi using certificate pfx


we have hosted dotnet core api in iis. we are using certificate authentication
in post method. we are getting 403 in postman.
Posted
Comments
Richard Deeming 26-May-23 4:45am    
There's a secret error somewhere in your secret code. You need to fix that.

Seriously, nobody can help you solve a problem you can't clearly describe, in code we can't see, running on a system we can't access.
Member 15418280 26-May-23 5:14am    
this is our controller

[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
[Authorize]
[HttpPost]
public string Post() => "The action works fine only with a certificate";
}


public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddTransient<certificatevalidation>();
services.AddAuthentication(CertificateAuthenticationDefaults.AuthenticationScheme).AddCertificate(options => {
options.AllowedCertificateTypes = CertificateTypes.SelfSigned;
options.Events = new CertificateAuthenticationEvents
{
OnCertificateValidated = context => {
var validationService = context.HttpContext.RequestServices.GetService<certificatevalidation>();
if (validationService.ValidateCertificate(context.ClientCertificate))
{
context.Success();
}
else
{
context.Fail("Invalid certificate");
}
return Task.CompletedTask;
},
OnAuthenticationFailed = context => {
context.Fail("Invalid certificate");
return Task.CompletedTask;
}
};
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApiCertificateAuth", Version = "v1" });
});
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

namespace WebApiCertificateAuth
{
public class CertificateValidation
{
public bool ValidateCertificate(X509Certificate2 clientCertificate)
{
string[] allowedThumbprints = { "310084D83EC974AEE7FC0B0D5175E11CA5E8DE6D" };
if (allowedThumbprints.Contains(clientCertificate.Thumbprint))
{
return true;
}
return false;
}
}
}
Jean Ferre 26-May-23 14:35pm    
I suggest you check everything is configured correctly on server and in certificate, and that the certificate is valid. If this is ok and error still remain you could try to split your pfx file into .crt and .key files, and use those in postman instead of the .pfx file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900