Hello everyone, I am going to share the code
sample for redirect http to https ASP.Net MVC projects using the different
types of techniques. The techniques types as given belows.
Types
1: You can add the below code in Global.asax.cs
for redirect
http to https.
protected void
Application_BeginRequest(Object
sender, EventArgs
e)
{
var httpRequest = HttpContext.Current.Request;
if
(httpRequest.IsSecureConnection.Equals(false) &&
httpRequest.IsLocal.Equals(false))
{
}
}
OR
Types
2: You can add the below code in Global.asax.cs
for redirect
http to https.
protected void
Application_BeginRequest(Object
sender, EventArgs
e)
{
switch
(Request.Url.Scheme)
{
case "https":
Response.AddHeader("Strict-Transport-Security", "max-age=300");
break;
var httpsPath = "https://" + Request.Url.Host
+ Request.Url.PathAndQuery;
Response.Status = "301 Moved
Permanently.";
Response.AddHeader("Location", httpsPath);
break;
}
}
OR
public class SSL_Filters : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if
(!filterContext.HttpContext.Request.IsSecureConnection)
{
var requestURL =
filterContext.HttpContext.Request.Url.ToString().Replace("http:", "https:");
filterContext.Result = new RedirectResult(requestURL);
}
}
}
Some other resources links as given below.
http://www.c-sharpcorner.com/uploadfile/niradhip/convert-your-site-from-http-to-https-after-installing-ssl-certificate/
http://stackoverflow.com/questions/47089/best-way-in-asp-net-to-force-https-for-an-entire-site
http://stackoverflow.com/questions/4945883/how-to-redirect-http-to-https-of-mvc-application-iis7-5
http://www.c-sharpcorner.com/uploadfile/niradhip/convert-your-site-from-http-to-https-after-installing-ssl-certificate/
http://stackoverflow.com/questions/47089/best-way-in-asp-net-to-force-https-for-an-entire-site
http://stackoverflow.com/questions/4945883/how-to-redirect-http-to-https-of-mvc-application-iis7-5