A way to find out if a WCF service is called via HTTP or HTTPS?

Say you have a WCF service available through HTTP and HTTPS, but you want only certain methods available with HTTPS. How to check if the current request is HTTPS? Since the HttpContext is empty, you can't just check the HttpContext.Current.Request.IsSecureConnection - any other ideas? Thanks in advance.

+3
source share
4 answers

Note that WCF applications can also be hosted as a Windows service, without ASP.NET at all, in which case there is no such thing as “safe” and “insecure”. This is one of the reasons why WCF is not trying to make this information available.

, WCF ASP.NET, HttpContext.Current.

, , , , SSL- . , , ; , , , , .

" " - , , , SSL, , .

+5

( @Aaronaught ...)

OperationContext.Current.RequestContext.RequestMessage.Headers.To.Scheme

"http" "https" (msdn).

+2

HTTPS is served through a different HTTP port. Assuming you know the ports, perhaps checking this will suffice.

0
source

What about:

var iwrc = WebOperationContext.Current.IncomingRequest;
var isHttps = iwrc.UriTemplateMatch.BaseUri.Scheme.Equals("https", StringComparison.InvariantCultureIgnoreCase);

It is very simple, does not rely on port numbers and works fine on a WCF server with its own hosts (without IIS in the site ).

0
source

Source: https://habr.com/ru/post/1735831/


All Articles