So testen Sie die Zertifikatauthentifizierung für APIs im PostmanC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 So testen Sie die Zertifikatauthentifizierung für APIs im Postman

Post by Anonymous »

Ich bin neu in der Zertifikatauthentifizierung von APIs. Mit Hilfe des Internets kann ich den Code erstellen, um das Zertifikat als TLS -Handshake zu akzeptieren, wenn die API aufgerufen wird. Jetzt habe ich meine PEM -Datei in den Postboten (Einstellungen >> Zertifikate) hochgeladen und die API lokal getestet (localhost). Mein Haltepunkt trifft, aber ich finde kein Zertifikat an APIs. Wie kann ich diese Zertifikatauthentifizierung für meine APIs konfigurieren? < /P>
Entschuldigung, wenn meine Frage verwirrend war. Ich habe mein Bestes versucht, es in Worte zu setzen.

Code: Select all

public static IServiceCollection AddCertificateAuthentication(this IServiceCollection services) {
services.AddAuthentication(CertificateAuthenticationDefaults.AuthenticationScheme).AddCertificate(options = >{
options.RevocationMode = X509RevocationMode.NoCheck;
options.AllowedCertificateTypes = CertificateTypes.All;
options.Events = new CertificateAuthenticationEvents {
OnCertificateValidated = context = >{
var cert = context.ClientCertificate;
if (cert == null) {
return Task.FromResult(AuthenticateResult.Fail("No client certificate provided."));
}

// Extract Common Name (CN) from Subject
var cn = cert.Subject.Split(',').FirstOrDefault(part = >part.Trim().StartsWith("CN=")) ? .Split('=')[1];

if (string.IsNullOrEmpty(cn)) {
return Task.FromResult(AuthenticateResult.Fail("Invalid certificate: CN not found."));
}

var claims = new[] {
new Claim(ClaimTypes.Name, cn),
};

var identity = new ClaimsIdentity(claims, CertificateAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, CertificateAuthenticationDefaults.AuthenticationScheme);

return Task.FromResult(AuthenticateResult.Success(ticket));
}
};
});

services.AddAuthorization(options = >{
options.AddPolicy("RequireCertificate", policy = >{
policy.AddAuthenticationSchemes(CertificateAuthenticationDefaults.AuthenticationScheme);
//policy.RequireAuthenticatedUser();
policy.RequireClaim(ClaimTypes.Role);
});
});
return services;
}
< /code>
startup.cs
services.AddCertificateAuthentication();
< /code>
 program.cs
public static IHostBuilder CreateHostBuilder(string[] args) = >Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder = >{
webBuilder.ConfigureKestrel(options = >{
options.ConfigureHttpsDefaults(httpsOptions = >{
httpsOptions.ClientCertificateMode = ClientCertificateMode.AllowCertificate;
});
});

webBuilder.UseStartup < Startup > ();
}).UseLoggingFramework();
}
Postman

< BR />

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post