Redirect URL To HTTPS (C#)
If you need to make sure that the current URL goes to HTTPS, then call this function.
If you need to make sure that the current URL goes to HTTPS, then call this function.
public void RedirectToHTTPS() { //this is the current url System.Uri currentUrl = System.Web.HttpContext.Current.Request.Url; //don't redirect if this is localhost if (!currentUrl.IsLoopback) { // Check if we need to redirect and not already in https if (!currentUrl.Scheme.Equals(Uri.UriSchemeHttps, StringComparison.CurrentCultureIgnoreCase)) { //build the secure uri System.UriBuilder secureUrlBuilder = new UriBuilder(currentUrl); secureUrlBuilder.Scheme = Uri.UriSchemeHttps; //use the default port. secureUrlBuilder.Port = -1; //redirect and end the response. System.Web.HttpContext.Current.Response.Redirect(secureUrlBuilder.Uri.ToString()); } } }