ASP.net check if page is http or https

2020-05-21 07:59发布

问题:

I have a web application hosted on multiple servers some of which are on https. How can I check from code behind if a page is currently in http or https?

回答1:

You can refer to the Request.IsSecureConnection property on the HttpRequest class. For a full reference outside a page, user control or alike, use HttpContext.Current.Request.IsSecureConnection.



回答2:

Page.Request.Url.Scheme

works as well. It returns http, https, etc.

Ref: http://msdn.microsoft.com/en-us/library/system.uri.scheme.aspx



回答3:

Use - HttpContext.Current.Request.IsSecureConnection



回答4:

Alternatively:

Request.ServerVariables["SERVER_PROTOCOL"];


回答5:

Update for Aspnet Core 2.0, now, you should use Request.IsHttps inside your controllers.



回答6:

Try this,

aCookie.Secure = HttpContext.Current.Request.IsSecureConnection


回答7:

In .net core I use:

Context.Request.Scheme == Uri.UriSchemeHttps


标签: asp.net https