Wednesday, 25 December 2013

Identifying https or SSL connection in load balancing environment in ASP.NET

Leave a Comment
In my current project, I’ve got one situation where I’ve to found whether URL is loading over http or https protocol. I’ve used below code to determine URL protocol:
string urlProtocol = HttpContext.Current.Request.Url.Scheme.ToLower();
Above code was working as expected in development server. For example :

1.)    URL : http://www.abc.com
value of urlProtocol = http
2.)    URL : https://www.abc.com
value of urlProtocol = https

However, code was failing in Production (LIVE) servers. Production servers are setup in load balancing environment (also known as Web Farm environment). I was getting below result in LIVE environment:

1.)    URL : http://www.abc.com
value of urlProtocol = http
2.)    URL : https://www.abc.com
value of urlProtocol = http  (which is incorrect)

Finally after spending few hours on this issue, I’d finally found the solution. In our production load balancing clustered hosting environment, the method for SSL detection (https) is a little different to what you might be familiar with.  We have separate SSL load balancer to enable SSL functionality across multiple web servers in a clustered environment. SSL is configured at load balancer level which means that SSL certificate for www.abc.com is installed on load balancer directly.

Therefore in this type of load balancing environment, the SSL connection stops at the load balancer level. Thus you can never test for an SSL connection using standard methods such as:
HttpContext.Current.Request.IsSecureConnection
HttpContext.Current.Request.Url.Scheme
Request.ServerVariables("HTTPS")
HttpContext.Current.Request.Url.AbsoluteUri.Contains("https://")
A custom header field gets inserted into the header to assist in identifying a secure request (https) in a load balancing environment.  I’ve checked with my hosting provider and ask them to insert a header field such as HTTP_X_FORWARDED_PROTO. This header field can vary for different hosting scenarios. It can be X-FORWARDED-PROTO or HTTP_X_FORWARDED_PROTO or HTTP_CLUSTER_HTTPS etc. Please make sure that your SSL load balancer is actually inserting these header fields into header. You can use below code to identify all server variables for a site:
foreach (string var in Request.ServerVariables)
{
  Response.Write(var + " " + Request[var] + "
");
}
Above code will list all server variables available for a site. In my case HTTP_X_FORWARDED_PROTO was the desired server variable. I’ve used below code to determine https or SSL connection:
bool isSecureConnection = String.Equals(Request.ServerVariables["HTTP_X_FORWARDED_PROTO"], "https", StringComparison.OrdinalIgnoreCase);
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_PROTO"] which will be set to 'HTTPS' if the user is accessing web page over SSL and will be null if the user is browsing over http.

Comments and suggestions are most welcome. Happy coding! 
Read More...

Friday, 13 December 2013

Adding Custom Language Code in Sitecore

Leave a Comment
In my current project, I have got one situation where I have to use Serbian language for one of our sites. On further investigation, I have found that Predefined Language Code for Serbia is not coming up in drop down of Globalization Control Panel in Sitecore.
Thus I have decided to add custom Language-Country Code sr-RS for Serbia. Unfortunately, I was getting below error while adding custom Language-Country Code sr-RS.
“The name “sr-RS” is not a valid or supported culture identifier.”
sr-RS is not a valid Language-Country Code.  Sitecore uses culture mechanisms of the .Net Framework. Following Language-Country Codes are available for Serbia in .Net Framework 4.0:


Language-Country codeCulture
sr Serbian
sr-Cyrl Serbian (Cyrillic)
sr-Cyrl-BA Serbian (Cyrillic, Bosnia and Herzegovina)
sr-Cyrl-CS Serbian (Cyrillic, Serbia and Montenegro (Former))
sr-Cyrl-ME Serbian (Cyrillic, Montenegro)
sr-Cyrl-RS Serbian (Cyrillic, Serbia)
sr-Latn Serbian (Latin)
sr-Latn-BA Serbian (Latin, Bosnia and Herzegovina)
sr-Latn-CS Serbian (Latin, Serbia and Montenegro (Former))
sr-Latn-ME Serbian (Latin, Montenegro)
sr-Latn-RS Serbian (Latin, Serbia)
In my case, sr-Latn-RS is the appropriate Language Country Code for the website. You can add any .Net framework supported Culture Code as per your requirement. Below are the steps of adding custom Language Country Code (sr-Latn-RS) for Serbia in Sitecore:
  1. Login to Sitecore Instance: Desktop Mode.  
  2. Click on Sitecore Menu -> Control Panel -> Globalization -> Add a new language
  3. You’ll get following pop up screen where you can add custom Language Code in Sitecore.
  4. Enter sr in Language textbox and Latn-RS in Country/Region Code textbox. Click on Next.

  5. Add file path of Spellchecker dictionary. This is optional step. You can ignore this step and click on Next without entering Spellchecker file path. You can read more about Adding Dictionaries to the Spellchecker.
  6. Click on Finish.
  7. You can see Sitecore Language Item at /sitecore/system/Languages/sr-Latn-RS path in Sitecore content tree.
You can also try The Language Registration tool which allows to register a language in Sitecore that doesn’t have the corresponding language in .Net Framework. This tool is now available in Sitecore Market place.
Comments and suggestions are most welcome. Happy coding! 
Read More...