Forcing SSL on ASP.NET from http to https and vice-versa

SSL

Lets assume we have a shopping chart web app, to create a safe environment for your buyers you need to run the payment page on https. So here is what we will need to do….

In your master page (assuming you have one), read through and paste the following code in the page load or page init:

protected void Page_Load(object sender, EventArgs e)
{

//if on https, secure = true;

bool secure = HttpContext.Current.Request.IsSecureConnection;

//Path of the page which requires SSL (https) – CHANGE TO OWN PATH

string paymentPagePath = “/PaymentGateway/default.aspx”;

// Server name or localhost if running locally

string serverName = Request.ServerVariables[“SERVER_NAME”];

// Path of the current page
string scriptName = Request.ServerVariables[“SCRIPT_NAME”];

// Query String if any
string queryString = Request.ServerVariables[“QUERY_STRING”];

// If we are on the page which requires SSL
if (absolutePath.Contains(paymentPagePath))
{

// If it is not currently using https
if (!secure)
{

if (Request.ServerVariables.Get(“HTTP_CLUSTER_HTTPS”) == null)
{

string xredir__, xqstr__;

// Build redirect Link using https 

xredir__ = “https://” + serverName;
xredir__ += scriptName;
xqstr__ = queryString;

if (xqstr__ != “”)
xredir__ = xredir__ + “?” + xqstr__;

// Redirect to same page using https

Response.Redirect(xredir__);

}

}

}

 // else if not of page which requires SSL

// if page is using SSL, we need to redirect to http
else if (secure)
{

string xredir__, xqstr__;

xredir__ = “http://” + serverName;
xredir__ += scriptName;
xqstr__ = queryString;

if (xqstr__ != “”)
xredir__ = xredir__ + “?” + xqstr__;

// Redirect to same page using http and not https

Response.Redirect(xredir__);

}

}

JavaScript Date Format dd/mm/yyyy

JavaScript String Date format dd/mm/yyyy hh:m:s  using a 24 hour clock:

<script>

function formatDate() {
var d = new Date();
var dd = d.getDate();
var mm = Number(d.getMonth() + 1);
var yyyy = d.getFullYear();

if (mm.toString().length == 1)
mm = “0” + mm;

var hh = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();

if (hh.toString().length == 1)
hh = “0” + hh;

return mm + “/” + dd + “/” + yyyy + ” ” + hh + “:” + m + “:” + s;
}

</script>

for meridian timing / 12 hour clocks ……

Continue reading

Get Query String using only JavaScript

Sending Query String URLs

lets assume we will have a link with a user’s id as a query string using ? to indicate the query string, id as a parameter name and =5842 to assign the value to the parameter:

<a href=”mypage.com/index.htm?id=5842“> Username </a>

Note: you can send multiple parameters using & as a seperator, example:

?id=5842&username=user1&password=psd

Loading Query String Values:

<script>

var urlParameters;

var UserID

//retrieving query string parameters
(window.onpopstate = function () {

var queryStringMatch,
regexReplaceSymbols = /\+/g,
search = /([^&=]+)=?([^&]*)/g,
decodeURI = function (s) { return decodeURIComponent(s.replace(regexReplaceSymbols , ” “)); },
queryString = window.location.search.substring(1);

urlParameters = {};
while (queryStringMatch= search.exec(queryString))
urlParameters[decodeURI(queryStringMatch[1])] = decodeURI(queryStringMatch[2]);

})();

//checking if parameter id exists and if it contains any value

if (urlParameters[“id”] != null && (urlParameters[“id”] != “”)){

UserID = urlParameters[“id”]; //assigning the parameters value to UserID

doSomething();

}

function doSomething()

{

//You can use UserID here now which contains the user’s id read from the query string

}

</script>