Friday, July 10, 2009

http to https conversion

protected void Page_Init(object sender, EventArgs e)
{
string fileName = System.IO.Path.GetFileName(HttpContext.Current.Request.FilePath);
//File name that you want to give Scheme Http to Https
if (fileName == "Register.aspx" || fileName == "MyAccount.aspx")
{
HttpToHttpsConvertor(HttpContext.Current.Request.Url, true);
}
else if (System.IO.Path.GetExtension(HttpContext.Current.Request.Path) == ".aspx")
{
//Remove Scheme Https to Http
HttpToHttpsConvertor(HttpContext.Current.Request.Url, false);
}
}

protected static void HttpToHttpsConvertor(Uri uri, Boolean isHttps)
{
UriBuilder secureUriBuilder = new UriBuilder(uri);

if (isHttps)
{
if (!HttpContext.Current.Request.IsLocal)
{
if (secureUriBuilder.Scheme == Uri.UriSchemeHttp)
{
secureUriBuilder.Scheme = Uri.UriSchemeHttps;
HttpContext.Current.Response.Status = "301 Moved Permanently";
secureUriBuilder.Port = 443;
HttpContext.Current.Response.AddHeader("Location", secureUriBuilder.ToString());
HttpContext.Current.Response.Redirect(secureUriBuilder.ToString());
}
}

}
else
{
if (!HttpContext.Current.Request.IsLocal)
{
if (secureUriBuilder.Scheme == Uri.UriSchemeHttps)
{
secureUriBuilder.Scheme = Uri.UriSchemeHttp;
secureUriBuilder.Port = 80;
HttpContext.Current.Response.AddHeader("Location", secureUriBuilder.ToString());
HttpContext.Current.Response.Redirect(secureUriBuilder.ToString());
}
}
}
}

Error 1

Data Transfer Interrupted

The connection to www.XXXXX.com:80 was interrupted while the page was loading.

The browser connected successfully, but the connection was interrupted while transferring information. Please try again.
  • Are you unable to browse other sites? Check the computer's network connection.
  • Still having trouble? Consult your network administrator or Internet provider for assistance.

Rectification:

Godaddy will provide a port address for SSL.
secureUriBuilder.Port = 443;

Note:

We can get all the status code http://status-code.com/



Error 2

XML Parsing Error: no element found
Location: http://www.XXXXX.com:443/Home.aspx
Line Number 1, Column 1:
^



Rectification:

Check whether you gave another port address for unsecure form like this.
secureUriBuilder.Port = 80;


No comments: