9

I would like to gain more information of a current TLS/SSL request in an ASP.NET Core MVC Controller.

using Microsoft.AspNetCore.Mvc;

namespace WebApp.Controllers
{
    public class HomeController : Controller
    {        
        public IActionResult About()
        {
            if (HttpContext.Request.IsHttps)
            {
                // How to get more information about the transport layer ?

                ViewData["Message"] = "Connection via TLS/SSL - ... missing details ...";
            }

            return View();
        }        
    }
}

Is there a way to access the properties like used TLS version, cipher and so on ?

I know that this is not possible in ASP.NET MVC as stated in Check ssl protocol, cipher & other properties in an asp.net mvc 4 application. Perhaps the new framework with Kestrel offers this kind of information I was not able to find so far.

Community
  • 1
  • 1
Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67
  • No, this information is not yet available. You can request it here: https://github.com/aspnet/kestrelhttpserver – Tratcher Aug 19 '16 at 23:31

2 Answers2

3

You can get those values using the Features property from the HttpContext. If you call:

var tlsHandshakeFeature = request.HttpContext.Features.Get<ITlsHandshakeFeature>();

ITlsHandshakeFeature will give you the TLS version (Protocol property), the CipherAlgorithm, among many other things.
You can also Get the ITlsConnectionFeature which will give you the certificates.

hardkoded
  • 18,915
  • 3
  • 52
  • 64
0

The other answer should now be valid for .NET 8, as it can be seen in this PR. However, if you are using an older version, you can use the following (found in the linked PR itself).

var sslStream = context.Features.Get<SslStream>();
var sslProtocol = sslStream.SslProtocol;
var cipherSuite = sslStream.NegotiatedCipherSuite;
Sayan Pal
  • 4,768
  • 5
  • 43
  • 82