Integrating Windows Live Authentication in your application

For one of my pet projects I am writing, I was contemplating how to integrate Windows Live Authentication with my standalone application, similar to Windows Live Messenger. Turns out, it is really easy! Below are the three steps that were needed for the integration:

  1. Download the Windows Live Client SDK from here and install it.
  2. Create a new project and add reference to Microsoft.WindowsLive.Id.Client assembly
  3. Write code similar to the one below:

using System; using Microsoft.WindowsLive.Id.Client;

namespace WLAuthenticate { class Program { [STAThread] static void Main(string[] args) { // Attempt user authentication Console.WriteLine("Enter your passport email address: "); string strPassportEmail = Console.ReadLine(); if (String.IsNullOrEmpty(strPassportEmail)) { Console.WriteLine("Enter valid address!"); return; }

        // Got the details - attempt to authenticate
        IdentityManager idMgr = null;
        Identity curID = null;

        // Create the identity manager
        idMgr = IdentityManager.CreateInstance("OrganizationName;name@company.com;ClientAuthSample",
            "Windows Live Client Authentication Sample App");

        // and now create the user's identity from it..
        curID = idMgr.CreateIdentity(strPassportEmail);
        bool fAuthenticated = false;

        if (curID != null)
        {
            if (curID.SavedCredentials == CredentialType.UserNameAndPassword)
            {
                // the credentials are persisted in local store and we can authenticate
                fAuthenticated = curID.Authenticate(AuthenticationType.Silent);
            }
            else
            {
                // Credentials not available locally, so show the signin dialog...
                curID = idMgr.CreateIdentity();

                // This will prompt for credentials to authenticate...
                fAuthenticated = curID.Authenticate();
            }

            if (fAuthenticated)
            {
                Console.WriteLine("Authenticated with Windows Live!");
                Console.WriteLine("Your username is {0}", curID.UserName);
                
                // Close the authentication
                curID.CloseIdentityHandle();
            }
        }
        
    }
}

}

Easy, isn't it :) ?

The only thing to be kept in mind is that if the passport authentication details are not persisted on the machine where authentication is attempted, then you will need to invoke Identity.Authenticate method without any arguments. This overload uses COM subsystem to display the Windows Live signin dialog to attempt authentication and requires the calling thread to be in STA mode and thus, tagged with STAThread attribute. Hence, I have set this attribute to the Main method above. Not setting this attribute will result in a ThreadStateException being raised.

This article is part of the GWB Archives. Original Author: Gaurav Khanna

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.