C# extension method to “prettyify” your xml output

Here is simple c# extension method to format your XML data. This is useful for logging xml data to trace files, etc:

public static class XmlDocumentPrettyExtension
    public static string Prettyify(this XmlDocument xmlDocument)
        var stringWriter = new StringWriter(new StringBuilder());
        var xmlTextWriter = new XmlTextWriter(stringWriter) { Formatting = Formatting.Indented };
        return stringWriter.ToString();
By |November 10th, 2014|Coding|0 Comments

Get SqlConnection from DbContext

I’ve been using Entity Framework for sometime now. But we all know using EF for everything may not be the best option. I decided to use Dapper for certain db queries.

Dapper requires an open SqlConnection object, I didn’t want to have to manage another connection string in my web.config so I looked at options for converting an EF connection string to an ADO connection string.

After some research I came up with the following:

private SqlConnection GetSqlConnection(DbContext dbContext)
    var ec = dbContext.Database.Connection;
    var adoConnStr = ec.ConnectionString;
    return new SqlConnection(adoConnStr);
By |October 23rd, 2014|Coding|0 Comments

Creating aliases in git

If you are like me and use git bash, powershell cmd or some other command line utility for git you quickly learn there is a lot of keystrokes to do repetitive tasks. Creating aliases in git is a convenient way to minimize the amount of keystrokes you will have to make. Here are some alias I have setup that I use on a daily basis:

Alias Name Configuration Setup Example Usage Description
git b git config –global alias.b branch git b master Allows you to easily switch branches
git co git config –global checkout git co new_feature Checkout a new branch
git undo git config –global alias.undo “reset HEAD~1 –mixed” git undo Undo the last commit.

Each time you create an alias you are adding it to one of 3 config files: local, system and global. In the examples above I add it to the global config file. To learn more about these config files take a look here: git-config

By |August 6th, 2014|Coding|0 Comments

Opinionated AngularJS styleguide for teams

Excellent article explaining different styles of writing angularjs modules, controllers:

By |July 24th, 2014|Coding|0 Comments

Using Azure Powershell to start and stop a VM

Using an Azure VM is a convenient way to test new software without having to corrupt your own terminal or using more resources for a virtual instance using Virtual Box, VMWare, etc.

As you may already know if a VM is stopped there are no charges for computing cost. However you will still incur a charge for the storage used by the VM (which is significantly less). Using a powershell script to manage the state of these VMs are helpful. I have the shutdown powershell script on a scheduler that runs nightly.

In order to use azure powershell you must do the following:

  • Install Azure PowerShell.
  • Connect to your subscription within Azure Powershell

Fortunately Microsoft has documented this process in an article located at:

After installing feel free to try the following code (make sure to replace “MY_COMPUTER” with the name of your VM:

Start-AzureVM -ServiceName "MY_COMPUTER" -Name "MY_COMPUTER"

Stop-AzureVM -ServiceName "MY_COMPUTER" -Name "MY_COMPUTER" -Force
By |July 16th, 2014|Coding|0 Comments

Generating a certificate and private key using openssl

Sometimes you need to generate a quick certificate to test your app. Using openssl you can easily accomplish this, take a look at my example below:

# Generate Cert and Key in seperate file
# Provide the .cert to the end user
openssl req -new -newkey rsa:1024 -days 365 -nodes -x509 -keyout QA.key -out QA.cert
# Merge cert and private key to make a p12 file, this file will be used by .NET's X509Certificate2 class, The private key will be accessible via the "PrivateKey" property
openssl pkcs12 -export -in qa.cert -inkey qa.key -out qa.p12 -name "QA"

Got a question? Send me a message on twitter: @tekguy

By |July 8th, 2014|Coding|0 Comments

Logging off WS-Federation by creating a wsignout1.0 message

Recently I integrated my web app with Azure ACS but was having a difficult time signing out of ACS (deleting my cookie off the server). The following code will create a wsignout1.0 message. Essentially it will construct a url with the action parameter set to “wsignout1.0”. An additional parameter “wreply” allows you to specify a url to redirect to after you have been signed out:

public ActionResult LogOff()
    // Load Identity Configuration
    FederationConfiguration config = FederatedAuthentication.FederationConfiguration;
    // Get wtrealm from WsFederationConfiguation Section
    string wtrealm = config.WsFederationConfiguration.Realm;
    string wreply;
    // Construct wreply value from wtrealm (This will be the return URL to your app)
    wreply = wtrealm;
    // Read the ACS Ws-Federation endpoint from web.Config
    // something like "https://<your-namespace>"
    string wsFederationEndpoint = ConfigurationManager.AppSettings["ida:Issuer"];
    SignOutRequestMessage signoutRequestMessage = new SignOutRequestMessage(new Uri(wsFederationEndpoint));
    signoutRequestMessage.Parameters.Add("wreply", wreply);
    signoutRequestMessage.Parameters.Add("wtrealm", wtrealm);
    string signoutUrl = signoutRequestMessage.WriteQueryString();
    return this.Redirect(signoutUrl);

Got a question? Send me a message on twitter: @tekguy

By |July 5th, 2014|Coding|0 Comments

Using AngularJs with a jQuery background

Most web developers today have experience using jQuery. If you are new to AngularJs and would like to start learning it I would suggest reading this post on Stack Overflow before reviewing any Angular tutorials. It will help clear up a lot of issues when trying to use both (I don’t recommend it).

By |June 30th, 2014|Coding|0 Comments

A visual explanation of SQL joins

To get a better understanding of SQL joins take a look at Coding Horror’s venn diagram display: Click here

By |February 11th, 2014|Coding|0 Comments

Implementing VisibilityProvider in MvcSiteMapProvider

If you are coming from MvcSiteMapProvider version 3 you will notice some changes in version 4. One of the key changes in version 4 is using app setting key/values instead of the SiteMap node. To learn more about new features in version 4 click here.

Implementing a visibility provider in version 4 is simple. It requires a couple small steps:

  1. Creating a class and derive it from “SiteMapNodeVisibilityProviderBase”.
  2. Add a setting to your web.config file.
  3. Update the site map file.


Creating our visibility class

Configuring web.config

Take caution and matchup your namespaces and classes correctly with the sample code I provided. Add a new app setting key/value:

<add key="MvcSiteMapProvider_DefaultSiteMapNodeVisibiltyProvider" value="MyCompany.Web.MyCompanySiteMapVisibilityProvider, MyCompany.Web" />

Making adjustments to sitemap

Finally we are going to make some adjustments to our sitemap file, use the following node as an example, take note of the visibility attribute:

<mvcSiteMapNode title="My Page I want to hide" controller="Home" action="HidePage" visibility="false">


I hope this helps, if you have any questions please contact me on twitter: @tekguy

By |January 9th, 2014|Coding|0 Comments