Customize bash in Mac terminal to show version control information

Highlights: I came across nice utility vcprompt which shows the git source control information in Mac OS bash terminal.

Install vcprompt using brew
brew install vcprompt

Now Add this code in .bashrc file in your home directory
You can search for different colors and use those as variables with “$” prefix.
You can pass different format options for vcprompt -f flag.

export PS1="\n\u:\w \[$RED\] \$(vcprompt -f [%b])\[$NO_COLOR\] \n→ "

Once you are done with above changes, either restart the terminal or run the command to load configuration file.

source ~/.bashrc

Result: Now You will see the project current branch and other information when you navigate to a folder which is under source control.

Xml DataType With Entity Framework

Requirement: Need to save an object collection in Sql Server Xml data type column.
Note: In Sql Server 2016 JSON support was added.

1. Employee and Address classes declared.
2. Created EmployeeMapping of type EntityTypeConfiguration which is used by DbContext.
Note: Addresses collection should be ignored in mapping to avoid key required error on Address Entity.

Note: it’s not full proof code.

deploy node web application to IIS

Requirement: Need to deploy node web application to on premise IIS 8.5.

1. Create a App Pool user and assign the user to IIS_IUSRS group. It’s easy to maintain permissions when we work with windows groups. This is a special group created for IIS and it makes life easy when we move applications to different windows servers.
2. Create Virtual directory for your application and give full permissions to IIS_IUSRS on this directory.
3. Now create a website or application for the virtual directory and set the app pool identity created in step 1.
4. Now download IISNode msi here. please pay attention on documentation relation to url rewrite.
5. After you install msi, run the batch file with admin privileges “C:\Program Files\iisnode\setupsamples.bat”.
6. Open IIS manager and expand default website, you should see samples are deployed under node application. This gave me pretty good understanding for me.
7. I have two different node web applications
Node application is serving both static and Api.
Node application is serving api only and static files are served by IIS.

Node Serving both static file and APi

Create web.config file in the virtual directory and add these settings.
rewrite: Any request comes to web site will be served by app.js
handlers: iisnode is a http handler which will execute app.js using node.

Note: what if don’t have app.js in the root directory? please see next section.
Please remove any hardcode port number from the index.js. port should passed from process “server.listen(process.env.PORT);”

Node Serving APi and IIS serving static content

Create web.config file in the virtual directory and add these settings.
default document: IIS serves default document from subfolder (new). index.html will load all css and other files.
rewrite: url that ends with api will be served by index.js which is under subfolder (server).

Note: Please remove any hardcode port number from the index.js. port should passed from process “server.listen(process.env.PORT);”

Hope this helps.

Using SQL Variables inside query in clause

Problem: I need to use a variable which should be used inside the in clause.

Case 1:
DECLARE @customerIds VARCHAR(200) = '123,456,789'
SELECT * From Customers Where Id in (????)
Case 2:
DECLARE @customerNames VARCHAR(200) = 'abc, def, ghi'
SELECT * From Customers Where FirstName in (????)

Solution Case 1:
Since already have comma separated integers, we should parse the variable as integer table.

CREATE FUNCTION [dbo].[StringSplitInt]
	@Idlist varchar(800),
	@delim char(1)
RETURNS @returntable TABLE
	Id int not null
	DECLARE @list varchar(800) = RTRIM(LTRIM(@Idlist)) + @delim;
	DECLARE @idString varchar(10), @id INT;
	WHILE (LEN(@list) > 0)
		SET @idString = SUBSTRING(@list, 1, CHARINDEX(@delim, @list,1)-1);
		SET @list = SUBSTRING(@list, LEN(@idString)+2, LEN(@list) - LEN(@idString)-1)
		INSERT @returntable(Id)


DECLARE @customerIds VARCHAR(200) = '123,456,789'
SELECT * From Customers Where Id in (SELECT Id from StringSplitInt(@customerIds))

Solution for Case 2:
Now we parse the string variable to table

CREATE FUNCTION [dbo].[StringSplitString]
	@Idlist varchar(800),
	@delim char(1)
RETURNS @returntable TABLE
	DECLARE @list varchar(800) = RTRIM(LTRIM(@Idlist)) + @delim;
	DECLARE @idString varchar(100);
	WHILE (LEN(@list) > 0)
		SET @idString = SUBSTRING(@list, 1, CHARINDEX(@delim, @list,1)-1);
		SET @list = SUBSTRING(@list, LEN(@idString)+2, LEN(@list) - LEN(@idString)-1)
		INSERT @returntable(Id)
		SELECT @idString


DECLARE @customerNames VARCHAR(200) = 'abc, def, ghi'
SELECT * From Customers Where FirstName in (Select Id from StringSplitString(@customerNames))

Note: You might get Collation mismatch error between the FirstName and Id Column returned by function. (I need to read a little more about this). I solved this problem by defining function column with our SQL Server collation setting.

Autocompletion for git commands

Requirement: it would be great to get autocompletion for git commands in mac terminal.

Install Git download

Now we need to download bash file with all commands. Open terminal and run this command from Home directory.

curl -O .git-completion.bash

Now check for .bash_profile in your home directory. open terminal
This command should show all invisible file also.


Create file .bash_profile if it doesn’t exist.

touch .bash_profile

Open file .bash_profile in text editor or editor of your choice.

open .bash_profile

Now append this code for .bash_profile file.

if [ -f ~/.git-completion.bash ]; then
    . ~/.git-completion.bash

Save it and close the terminal.

Open the terminal.
type > git h
This should complete command as “git help”

Verify 2:

You can get completion for git command options also.
git log –a
this should list all possible options start with “–a”.

Ionic 2 App Integration with Auth0 and Firebase

Problem Statement: Ionic 2 App authenticates using Auth0 and then using Auth0 token to firebase API as backend without using firebase authentication.

Solution: I have created base version of this project in GitHub. You can clone it from here.

1. Clone the project
2. npm install ionic cordova typescript -g
3. After cloning project, run npm install for all local packages to install.

Step 1:

I am using facebook authentication with Auth0, so first we need to create facebook app in developer console.

Once you got application Id and App Secret then add the following Auth0 callback url on successful authentication. Fore new Apps, you need to add facebook login by clicking on “Add product” and then go to “Settings” to add callback Url.

Step 2:

Create Firebase app in console.
Now we need to get Api key to integrate to Auth0. Click on seetings icon.

Now go to Service Accounts and then click “Generate Private Key” downloads JSON. we will use this information in Auth0 Configuration.

Next need to get firebase config, for this go to Overview-> Add to web. this configuration is used in appmodule.ts

Step 3:

Create Auth0 Account
Now go to social connection and select facebook. Enter application id,app secret, scope you need for your application.

Create new application with type “Native”. Change callback url if needed.

Now go to Addons tab and enable firebase. Fill information in settings from the json file got from firebase.

Now go to connections tab in your app, go and enable facebook. if you remember we enabled facebook in social connection and then we add facebook for each and every app we work.

Step 4:

Now we will go through some of the code.

src/auth0-variables.ts: You will set Auth0 App details in this file before you run the application.
src/app/app.module.ts: We will configure angular fire module with firebase configuration info as shown above.
login() method shows Auth0 screen.
constructor() This has authentication callback where we get Auth0 token and then we can get firebase access token.

Asp.Net Web API 2 Customize Token Expiration

Problem Statement: We need our Web API  to issue bearer tokens with different expiration based on type of the client (Web, Mobile and Desktop).

Solution 1: Let the WEB API always issue token with same expiration for every client. this is straight forward implementation done in application startup.

AccessTokenExpireTimeSpan: you can set this property based on the security needs of your application.

OAuthOptions = new OAuthAuthorizationServerOptions
                TokenEndpointPath = new PathString("/Token"),
                Provider = new ApplicationOAuthProvider(PublicClientId),
                AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(2),
                // In production mode set AllowInsecureHttp = false
                AllowInsecureHttp = true,
                AuthenticationMode = AuthenticationMode.Active,
                AuthenticationType = OAuthDefaults.AuthenticationType

// Enable the application to use bearer tokens to authenticate users

Solution 2: Let the Web API issue token with different expiration times based on the client type. we would give longer expiration for desktop clients.

Provider: we have custom implementation for the ApplicationOAuthProvider as shown in Solution 1. Now for each client id we can set different expiration.
AccessTokenExpireTimeSpan: You need to comment this in start up configuration as shown in solution becuase you are customizing it in provider class.

Note: I didn’t provide full implementation here.

public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider
    public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        string clientId;
        string clientSecret;
        //This AuthModel is my custom model that will be passed in OwinContext
        var authModel = new AuthModel();

        if (context.TryGetFormCredentials(out clientId, out clientSecret))
            if (clientId.Equals("appWeb", StringComparison.OrdinalIgnoreCase))
                context.Options.AccessTokenExpireTimeSpan = TimeSpan.FromHours(2);

                context.OwinContext.Set("oauth:client", authModel);
                context.Validated(clientId);// please validate client credentials.
            else if (clientId.Equals("mobile", StringComparison.OrdinalIgnoreCase))
                context.Options.AccessTokenExpireTimeSpan = TimeSpan.FromDays(3);
                authModel.ClientSecret = clientSecret;

                context.OwinContext.Set("oauth:client", authModel);
                context.Validated(clientId); // please validate client credentials.
                context.SetError("invalid_client", "Client credentials are invalid.");
            context.SetError("invalid_client", "Client credentials are invalid.");

        return Task.FromResult(0);

Happy Coding!

Parse IIS Logs with Log Parser.

Requirement: Need some statistical data from IIS logs

Log parser: This is from Lizard Labs and it’s not free tool. This is very powerful tool with amazing GUI. Log Parser From Lizard Labs

Log Parser(Command Line): This is from Microsoft and it’s free. Download

I have used this command line interface to process the log files into database. I can run multiple reports once the data is in database.


  1. Copy all the logs into separate directory.
  2. Open command prompt and go to “C:\Program Files (x86)\Log Parser 2.2”.
  3. Run the this statement to process logs and insert data into Sql Server.

LogParser "SELECT * INTO tablename FROM D:\Logs\*" -i:W3C -o:SQL -server:servername -database:databasename -driver:"SQL Server" -username:sa -password:**** -createTable:ON

Hint: you can find examples by running this command “logparser -h examples”.

Thank You

Code signing .Net assemblies with comodo certificate in Visual Studio

Requirements: Recently i got a requirement to publish code outside of our company as installation. We don’t want our users to see untrusted publisher error while using aour application.

Certificate from Comodo: You need to follow these steps to get (*.pfx) to sign the assemblies.

  1. Use only Internet explorer or Firefox . Chrome is not currently supported by Comodo.
  2. Create account and request for code signing certificate.
  3. After successful submission, you will receive email from comodo within 2 business days.
  4. Remeber, you need to use same system and same browser to install the certificate sent by Comodo.
  5. Once you install certifcate, you can export the certifcate as (*.pfx) file and sent the same to development team. Please secure this file with strong password.

As a developer: Follow these steps to sign the assemblies

  1. If you are getting error “Cannot find the certificate and private key for decryption.”. please follow steps to install your certificate. Follow Steps
  2. If you get error while signing “Cannot import the following key file: companyname.pfx. The key file may be password protected. To correct this, try to import the certificate again or manually install the certificate to the Strong Name CSP with the following key container name: VS_KEY_xxxxxxx”. now you can install key value pair to container by using below command."C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\sn.exe" i codesign.pfx VS_KEY_5FC66E4E834CAD9C
  3. In Visual Studio project properties, you can choose signing tab and then select the pfx to sign. i dont have luck with this approach, so i followed next step
  4. In Visual Studio project properties -> Build Events -> Post Build Scripts
    "C:\Program Files (x86)\Windows Kits\10\bin\x64\signtool.exe" sign /f "C:\anyfolder\codesign.pfx" /p password /tr "" "$(TargetDir)*.dll"
  5. Now all your project assemblies should be signed. you can open properties of an assembly to confirm whether digital signatures tab is visible.

Happy Coding!

Using Google Authentication in Windows Applications

Goal: We have windows application built using .Net and this application should be able to retrieve basic information about the user like name, email and his unique parameters like google id etc.

Google Libraries: Google has developed and maintaining libraries as nuget packages for Open Id authentication and services like Google Plus, Google Drive etc.

Note: you can search using “Google.Apis.{google service like plus, drive}”. I am currently using their “Google.Apis.Plus.v1” with version


  1. We need to create an application in Google developer console.
  2. Next create Project, Credentials (Client Id, Secret) and enable  necessary API permissions. I have enabled Google + Api

Note: For windows applications client secret is really not a secret, so we really no need to worry keep it in secret place.

Application Changes:

Step 1:

We have all the information need to authenticate our users by using GoogleWebAuthorizationBroker.

var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets() {ClientId = "your client id", ClientSecret = "your client secret" },
new[] {PlusService.Scope.PlusMe, PlusService.Scope.UserinfoProfile, PlusService.Scope.UserinfoEmail}, "me", CancellationToken.None);

Step 2:

After step 1, your application should open browser for user to authenticate and authorize your app to use requested scopes.  Now we use below code to create Person Resource.

Note: Since our app is designed to get currently logged in user, we need to query people resource with id as “me”.

// Create the service.
var service = new PlusService(new BaseClientService.Initializer()
HttpClientInitializer = credential,
ApplicationName = "Sample",
var peopleResource = service.People.Get("me");
var person = await peopleResource.ExecuteAsync();

Step 3:

Now person object has all the basic information like email, display name, profile url etc.

Happy Coding.