'VB.NET
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
'Require SSL
If (Request.UserHostName <> "127.0.0.1" _
AndAlso Request.UserHostName <> "localhost") Then
If Request.ServerVariables("HTTPS") = "off" Then
Dim redir As String = "https://" Request.ServerVariables("SERVER_NAME") Request.ServerVariables("SCRIPT_NAME")
If Request.ServerVariables("QUERY_STRING") <> "" Then
redir = "?" Request.ServerVariables("QUERY_STRING")
End If
Response.Redirect(redir)
End If
End If
End Sub
//C#
public void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.UserHostName != "127.0.0.1" && Request.UserHostName != "localhost")
{
if (Request.ServerVariables["HTTPS"] == "off")
{
string redir = "https://" Request.ServerVariables["SERVER_NAME"] Request.ServerVariables["SCRIPT_NAME"];
if (Request.ServerVariables["QUERY_STRING"] != "")
{
redir = "?" Request.ServerVariables["QUERY_STRING"];
}
Response.Redirect(redir);
}
}
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)