Setting and forwarding Client Cert directly from YARP
Is it possible for YARP to get a Certificate from disk/Cert Store and then forward with a response? Based on the information here: https://microsoft.github.io/reverse-proxy/articles/transforms.html#clientcert The client certificate should already be present on the connection. My interpretation is that the Client App/Caller needs to forward the Cert to YARP.
I also attempted to implement the AddCertificateForwarding middleware: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/certauth?view=aspnetcore-7.0 However, the Client Cert in never included in the request Header. This makes sense given that the middleware is instantiated once and not per request.
Yes
Here's an example:
https://damienbod.com/2022/02/07/using-blazor-with-a-yarp-downstream-api-protected-using-certificate-authentication/
Yes
Here's an example:
https://damienbod.com/2022/02/07/using-blazor-with-a-yarp-downstream-api-protected-using-certificate-authentication/
Saw this article and tried it. But I do not see a client cert in the response Header.
What's the purpose of a certificate in a response header? Client certs are supposed to be sent with the request from the client to YARP and then the server, not from the server or YARP to the client.
What's the purpose of a certificate in a response header? Client certs are supposed to be sent with the request from the client to YARP and then the server, not from the server or YARP to the client.
You are right, not the response but the request.
Let's assumed I have YARP running on a machine and making REQUESTS to a backend server. I need to add a Client Certificate to YARP so that the REQUEST from YARP are authenticated at the Backend Server. I do not want a cert included in the REQUEST to YARP but rather contained into YARP itself.
When a REQUEST is made to YARP, then YARP takes that REQUEST and include a CLIENT Cert and makes a request to the backend Server.
Oh, you want to authenticate the proxy to the destination with a client cert. Try this: https://microsoft.github.io/reverse-proxy/articles/http-client-config.html#configuring-the-http-client
Oh, you want to authenticate the proxy to the destination with a client cert. Try this: https://microsoft.github.io/reverse-proxy/articles/http-client-config.html#configuring-the-http-client
var clientCert = new X509Certificate2("path");
services.AddReverseProxy()
.ConfigureHttpClient((context, handler) =>
{
handler.SslOptions.ClientCertificates.Add(clientCert);
})
I get object not set to instance of an object:
I had the same error, which I fixed by calling
handler.SslOptions.ClientCertificates = new X509Certificate2Collection(clientCert);
I'm not sure if this is the correct fix, because it is the first time I'm using YARP
I had the same error, which I fixed by calling
handler.SslOptions.ClientCertificates = new X509Certificate2Collection(clientCert);I'm not sure if this is the correct fix, because it is the first time I'm using YARP
Yes, this was also my solution and you beat me to it. We need to instantiate a new X509CertificateCollection as you pointed out before adding any certificate to the collection. The documentation clearly omits this information.
Thank you also.
I had the same error, which I fixed by calling
handler.SslOptions.ClientCertificates = new X509Certificate2Collection(clientCert);I'm not sure if this is the correct fix, because it is the first time I'm using YARPYes, this was also my solution and you beat me to it. We need to instantiate a new X509CertificateCollection as you pointed out before adding any certificate to the collection. The documentation clearly omits this information.
Thank you also.
Where does this line of code go?
Hi there,
You can put this code in the .ConfigureHttpClient, after you call .AddReverseProxy().
In my case:
public void ConfigureServices(IServiceCollection services) {
var certCollection = LoadClientCertificates(appSettings.ClientCertificateThumbprint);
services
.AddReverseProxy()
.ConfigureHttpClient((context, handler) =>
{
handler.SslOptions.ClientCertificates = certCollection;
})
.LoadFromConfig(Configuration.GetSection("ReverseProxy"));
}
private static X509Certificate2Collection LoadClientCertificates(string thumbprint)
{
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
try
{
store.Open(OpenFlags.ReadOnly);
var certCollection = store.Certificates;
var signingCert = certCollection.Find(X509FindType.FindByThumbprint, thumbprint, false);
if (signingCert.Count == 0)
{
throw new FileNotFoundException($"Cert with thumbprint: '{thumbprint}' not found in local machine cert store.");
}
Log.Logger.Information("Loaded client certificate with thumbprint: {thumbprint} successfully", thumbprint);
return signingCert;
}
finally
{
store.Close();
}
}