Wednesday, November 27, 2013

Handling Dynamic Subdomains in ASP.NET MVC

I was doing some research work on implementing dynamic subdomains in an ASP.NET MVC, and I found an interesting article which explains the routing functionality and a trick to test it on local environment:

Thursday, November 14, 2013

Get Twitter Feeds

Twitter has recently changed the way to access the feeds for some security reason.  The old simple way of accessing it through RSS has been depicted.  The new method has few additional steps before you can access your precious tweets. 

Before accessing the tweets, you need to register your twitter account and get their API keys here:

Basically 4 keys are needed:
  1. Token
  2. TokenSecret
  3. ConsumerKey
  4. ConsumerSecret

public string GetTwitterFeed()
                string q = "twitterName";
                string resource_url = "";

                var oauth_token = ConfigurationManager.AppSettings["twitterAccessToken"];
                var oauth_token_secret = ConfigurationManager.AppSettings["twitterAccessTokenSecret"];
                var oauth_consumer_key = ConfigurationManager.AppSettings["twitterConsumerKey"];
                var oauth_consumer_secret = ConfigurationManager.AppSettings["twitterConsumerSecret"];

                // oauth implementation details
                var oauth_version = "1.0";
                var oauth_signature_method = "HMAC-SHA1";

                // unique request details
                var oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
                var timeSpan = DateTime.UtcNow
                    - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

                // create oauth signature
                var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +

                var baseString = string.Format(baseFormat,

                baseString = string.Concat("GET&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));

                var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                                        "&", Uri.EscapeDataString(oauth_token_secret));

                string oauth_signature;
                using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
                    oauth_signature = Convert.ToBase64String(

                // create the request header
                var headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " +
                                   "oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " +
                                   "oauth_token=\"{4}\", oauth_signature=\"{5}\", " +

                var authHeader = string.Format(headerFormat,

                ServicePointManager.Expect100Continue = false;

                // make the request
                var postBody = "q=" + Uri.EscapeDataString(q);//
                resource_url += "?" + postBody;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
                request.Headers.Add("Authorization", authHeader);
                request.Method = "GET";
                request.ContentType = "application/x-www-form-urlencoded";
                var response = (HttpWebResponse)request.GetResponse();
                var reader = new StreamReader(response.GetResponseStream());
                var objText = reader.ReadToEnd();
                //myDiv.InnerHtml = objText;/**/

                JArray jsonDat = JArray.Parse(objText);
                return Convert.ToString(jsonDat[0]["text"]);

            catch (Exception ex)
                throw ex;

The code above would fetch only one feed out of the list of feeds obtained.  The code highlighted in the bold text can be changed to fetch more tweets.

ASP.NET Force HTTPS for all the pages of website

Most of the secured sites have a scenario of permanently redirecting to HTTPS protocol, even if the user tries to access it through HTTP.  

You can easily achieve this by adding a small piece of code in your Application_BeginRequest event in Global.asax file.

  protected void Application_BeginRequest(Object sender, EventArgs e)
            if (ConfigurationManager.AppSettings["HTTPS"] == "1" && HttpContext.Current.Request.IsSecureConnection.Equals(false) && HttpContext.Current.Request.IsLocal.Equals(false))
                Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"] + HttpContext.Current.Request.RawUrl);

The Above code would exclude HTTPS redirection for local environment i.e. localhost too.