如何从在 Kestrel asp.net core 上执行 ClientCertificateValidation 的函数中获取 HttpContext?

Pau*_* W. 6 kestrel-http-server asp.net-core

I have an asp.net core self hosted application within Kestrel. Part of the .UseKestrel options within my Program.cs Main() function is to use the ClientCertificateValidation() functionality to have my own function validate client certificates.

This validation functionality is working fine but my custom validation function needs to be able to add an identity role to the connection that has been validated. I know normally you could just use dependency injection to get HttpContext but since this is a function pointed to within UseKestrel.ClientCertificateValidation within my Program.cs Main() function there is no way to inject the HttpContext dependency that I know of.

Does anyone know some other way to make something like dependency injection work in this scenario or another way to access HttpContext from within my custom certificate validation function? thank you

High level snippet so you can understand what is nested where:

public static void Main(string[] args)
    {
.UseKestrel(options =>
                    {
httpsoptions.ClientCertificateValidation = (cert,chain,errors) => certHelper.ValidateClientCert(cert,chain,errors, null);
}}

public bool ValidateClientCert(X509Certificate2 cert, X509Chain chain, SslPolicyErrors sslerrors, CertificateValidationConfig certvalidationconfig)
        {
           //How to get HttpContext in this function?
}
Run Code Online (Sandbox Code Playgroud)

小智 0

这是一个非常古老的问题,但仍然

答案是这是不可能的,这是设计使然客户端证书验证作为初始连接的一部分发生,只有在成功后您的应用程序才会读取实际请求