Nathan Totten's BlogThoughts and Experiences with Software Development.http://ntotten.com
Tue, 28 Jul 2015 21:47:07 +0000enhourly1http://0.gravatar.com/blavatar/4467e37d91705aa956f90e6c2e16e118?s=96Nathan Totten's Bloghttp://ntotten.com
Sublime Text Setup ScriptFri, 09 May 2014 00:00:00 +0000http://ntotten.com/2014/05/09/sublime-setup-script/
http://ntotten.com/2014/05/09/sublime-setup-script/One thing that I find odd about the default Sublime Text install on Windows is that you don't get the option to add subl to the path. On OS X this happens automagically. I wrote a simple script that I use on every new Windows machine to setup sublime the way I like. In addition to adding subl to the path, this also copies my sublime settings. Pretty simple script, but I find it useful.

]]>Deploying Static Content to Azure Storage with Visual Studio Online BuildMon, 24 Feb 2014 00:00:00 +0000http://ntotten.com/2014/02/24/deploying-static-content-to-azure-storage-with-vso/
http://ntotten.com/2014/02/24/deploying-static-content-to-azure-storage-with-vso/A common task for improving the performance a web application is to server your static content from a different domain from you app. This external domain could be simply another pointer to your web server, a CDN service, or in the case here Windows Azure Blob storage. Blob storage is a fast and super inexpensive place to host static content for your site. To that end, I wrote a very simple PowerShell script that deploys all of my .min.js files in my script folder to blob storage. This script can be used from a Visual Studio Online build definition in order to automate the deployment.

Note, this script assumes that your site (in this case MyWebSite) and the script itself are at the root of your source directory. If you have a different directory structure, you will need to adjust the paths.

Also, notice that I am referencing the Windows Azure PowerShell Cmdlets which are installed on the VSO build server.

I was recently working on a prototype for an internal project that I wanted to publish to Windows Azure Web Sites. Because the project is for Microsoft use only I needed to either setup authentication or filter by IP address. Authentication wasn't really what I wanted because I wanted the prototype to be easily shared within the company. Security wasn't super critical, but it needed to be good enough. My initial thought was to just use the <ipSecurity> configuration in web.config. However, I realized that this module isn't actually installed on Windows Azure Web Sites and that even if it was, it wouldn't work for my needs because sites in WAWS sit behind ARR servers.

The solution I ended up using was to create a simple MVC Action Filter that blocks requests from outside of a specific network range. Unfortunately, some of the utilities for calculating subnets are actually not part of .NET. However, I was able to find a nice set of extensions that gave me what I needed. Combining these extensions with a simple action filter was able to perform my required task.

You can find the full source to this action filter in this Gist. It contains both the IPFilterAttribute class as well as the IPAddressExtension.cs file.

Using the filter is easy. Simply add the filter to your controller and specific the IP address or network you want to allow.

There are a couple things worth noting as to how this filter actually works. First, since Windows Azure Web Sites sits behind a ARR server the IP address of the end user is not the IP address the request comes from. The end user's IP address is actually stored in the header "X-Forwarded-For". You can see how we get the ip address below.

Second, this is just a sample I wrote quickly. If you are using this for something that requires real security or solid error handling, please look over the code first.

]]>Windows Azure SDK for Node.js and CLI updated for Node.js 0.10.xSun, 14 Apr 2013 00:00:00 +0000http://ntotten.com/2013/04/14/windows-azure-sdk-for-nodejs-and-cli-updated-for-nodejs-010x/
http://ntotten.com/2013/04/14/windows-azure-sdk-for-nodejs-and-cli-updated-for-nodejs-010x/Good news! The Windows Azure Cross Platform CLI and the Windows Azure SDK for Node.js has been updated to support Node.js version 0.10.3 and above. You can now update your installation of node and Windows Azure tools to the latest and greatest.

Note: Make sure you are using 0.10.3 or greater. Guang Yang, who is a PM on the team that writes the SDK pointed out that there is a bug in earlier versions that can cause issues in the SDK.

To upgrade Node.js head over to nodejs.org and click the install button. In order to upgrade the Windows Azure CLI tools simply use npm as show below.

npm install azure-cli -g

Note: On OS X you will need to run this command as sudo to install globally.

To install the latest version of the Windows Azure SDK for Node.js in your project you can use npm as well.

npm install azure

An alternative option is to use our one-click installers to install the SDK and tools together. You can find the Node.js installer here.

]]>Using Node.js with Jekyll to Optimize Static ContentWed, 10 Apr 2013 00:00:00 +0000http://ntotten.com/2013/04/10/using-nodejs-with-jekyll-to-optimize-static-content/
http://ntotten.com/2013/04/10/using-nodejs-with-jekyll-to-optimize-static-content/I recently migrated my blog to Jekyll hosted on Github Pages. I did this primarily because I was sick of dealing with the web editor in Wordpress. Wordpress is a great blogging platform for many people, but I just wanted something simpler and more flexible. I have written about how to configure Jekyll on Windows in the past. This post is about optimizing the content of a Jekyll page using Node.js.

The reason, I am using Node.js is primarily because of cross-platform issues with many Ruby gems. I played around with Jekyll Asset Pipeline and it seemed like a great tool for combining and minifying static content, but it seems to have issues running on Windows. I use many different computers including several PCs (I love my Surface Pro) and a Macbook Air so cross-platform support is critical. I blog on both devices so my blog platform must work on each OS. Node.js is an obvious choice for this as it runs flawlessly on both platforms.

The second reason I chose to use Node.js rather than Ruby gems is because I am hosting this site on Github Pages to use their automatic Jekyll deployment. Unfortunately, Github Pages doesn't support Jekyll plugins so anything that does this level of customization must be done on my local machine anyway. This means it didn't really matter which tools I used.

Now, that you know why I am doing this. Lets talk about how. First, I decided to use node-minify to handle the actual combining and minifying of files. This node module works well, it is simple, and it works with several minification libraries like UglyJS, YUI Compressor, and Google Closure Complier. Since I avoid Java like the plague I am just using UglyJS. The optimization isn't quite as good as the other two, but it is enough for me.

Below you can see how node-minify can be used to combine and minify js and css files.

In addition to node-minify I also wanted something that would help me automate the various tasks of building the site. For this I chose Jake. Jake is a great tool that I have used on many other projects for creating build scripts.

On thing you will notice is that my static content is being created with a uuid version number. This way each time I publish my site I get a new version. Eventually, I am going to change this code to only perform the minification if the css or js files change. The issue with using a random name is that I need to somehow tell Jekyll which files to use. For this, I set a configuration value in the _config.yml file that Jekyll uses. You can see that file below. The version number gets updated every time I build my static content.

Now when I publish the site to Github pages, Jekyll will use the version number to setup the static content path and my site will be served with the smaller and optimized js and css files. You can see the full jakefile.js in my website's Github repositoryy.

]]>Scaling Out Socket.IO with Windows Azure Service BusFri, 05 Apr 2013 11:41:54 +0000http://ntotten.com/2013/04/05/scaling-out-socket-io-with-windows-azure-service-bus/
http://ntotten.com/2013/04/05/scaling-out-socket-io-with-windows-azure-service-bus/Socket.IO is a handy tool that makes it easy to build real-time communication into your application. If you haven't used Socket.IO before, I recommend reading the tutorial over on WindowsAzure.com. This post shows how you can scale out Socket.IO to multiple servers in order to handle many simultaneous connections by using Windows Azure Service Bus as a backing store.

The get started, you will need to create a service bus namespace and subscription. You can do this in the portal by clicking the "New" button and following the steps shown below.

After your topic is created, install the socket.io-servicebus module by running npm as shown below.

Note: I am installing my fork of the socket.io-servicebus module. The module is also available on npm, but there are updates to this fork for automatically managing subscriptions. These updates will be in the main version shortly. You can see the pull request for my updates here.

With the module installed, open up your application. For this demo, I am using an express web app, but you can use whatever frameworks you would like. Configure the service bus store for Socket.IO as shown below.

After you have configured the service bus store you are now ready to scale your application to use as many servers as required. You can download the full source of my sample application on Github.

]]>Password Caching when Publishing with Git to Windows Azure Web SitesThu, 04 Apr 2013 11:49:52 +0000http://ntotten.com/2013/04/04/password-caching-when-publishing-with-git-to-windows-azure-web-sites/
http://ntotten.com/2013/04/04/password-caching-when-publishing-with-git-to-windows-azure-web-sites/If you have setup git publishing on a Windows Azure Web Site you have noticed that our git endpoints use HTTPS rather than SSH. This means that you can't use an SSH key to authenticate and thus you will need to enter your password every time you publish to Windows Azure Web Sites via a git push. The good news is that Github has created a tool that allows credential caching of HTTPS git endpoints. This tool is compatible with both OS X and Windows and is very easy to setup.

You can see how to setup Github's password caching tool on their help documentation here. As their documentation mentions, you do need to ensure you are running Git version 1.7.0 or above.

You can see that after the password helper is setup we are prompted for credentials on the first commit, but on subsequent commits our cached credentials are used automatically.

First Push

Second Push

Hopefully that helps save a little time when developing on Windows Azure.

]]>Using Windows Azure Mobile Services with the Facebook SDK for Windows PhoneThu, 14 Mar 2013 14:10:19 +0000http://ntotten.com/2013/03/14/using-windows-azure-mobile-services-with-the-facebook-sdk-for-windows-phone/
http://ntotten.com/2013/03/14/using-windows-azure-mobile-services-with-the-facebook-sdk-for-windows-phone/As you may have seen, Windows Azure Mobile Services makes it super easy to add a cloud backend to your mobile apps. Mobile Services also supports authentication with OAuth providers like Facebook, Microsoft, and Twitter. I have had people ask me if it is possible to use Mobile Services with the Facebook SDK for .NET (formally named the Facebook C# SDK). In short, yes.

Authenticating with Facebook

Since our app is going to be authentication with Facebook we will likely want to ask the user for additional Facebook permissions such as posting to a wall or reading additional friend data. This currently isn't supported in the Mobile Services SDK. So the first thing we need to do is change how the initial authentication is performed. To this we are going to use the Facebook SDK for Windows Phone. The Facebook SDK for Windows Phone is a layer on top of the Facebook SDK for .NET that helps with authentication and other phone specific scenarios.****

Warning: The Facebook SDK for Windows Phone is currently in alpha so things will change and break as we move toward version 1.0.0.

Starting with our default Mobile Services implementation of authentication as show in this tutorial we need to make a few minor changes.

First, we need to install the Facebook SDK for Windows Phone. To do this use NuGet. Since the package is pre-release you will need to use the Nuget console.

Install-Package Facebook.Client -pre

Next, add a static property for FacebookSessionClient to the App.xaml.cs file. You can add this property right above the MobileServiceClient property.

As you can see, we are now using the FacebookSessionClient to perform the OAuth with Facebook. After the user authorizes the app with Facebook we then create a JToken object containing the Facebook Access Token and pass it to the MobileServiceClient's LogingAsync method. Mobile Services then makes a request to the Mobile Services REST API. On the server a new user is created and a Mobile Services authentication token is created. This token is returned to the client. Now the client is authenticated with both Facebook and Mobile Services.

Also, notice that the FacebookSessionClient's LoginAsync method allows us to pass in a string of permissions to request. In this case we are requesting the email, publish_stream, and friends_about_me permissions. You can change this list accordingly based on the needs of your app.

Using the Facebook Graph API

Now that we are authenticated and have a Facebook access token we can perform requests on the Facebook Graph API using the Facebook SDK for Windows Phone. In this case I am going to request a list of friends including their "About Me" details as we have the "friends_about_me" permission.

To get the Facebook access token simply access the AccessToken property on the FacebookSession object which we have in our app at fbSession. You can see below how you can make this Graph API request using the FacebookClient object.

Now you are free to interact with both the Facebook Graph API and the Mobile Services API as an authenticated user. You could also use the Facebook SDK for Windows Phone to request additional permissions if your application requires them.