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