Live Services SDK
How to Set Up Authentication

Before you call any Windows Live ID APIs, you must first add a reference to the component in Visual Studio as described in the following procedure.

To add a reference

  1. Open your project in Visual Studio.

  2. In Solution Explorer (shown here), right-click References, and then click Add Reference.

    Bb676892.3e9bad06-26f9-4012-ad1d-c8013e52fa8b(en-us,MSDN.10).gif
  3. In Component Name, locate Microsoft.WindowsLive.ID.Client, select it, and then click OK.

  4. Expand the References node, and then click Microsoft.WindowsLive.ID.Client.

  5. In the Properties window (shown here), set the Specific Version property to false.

    Bb676892.99f311cf-735a-4cdf-8dde-249452afaa67(en-us,MSDN.10).gif

To use the reference in code, you must include it with either the using directive (in C#) or an Imports statement (in Visual Basic). From the sample application:

using Microsoft.WindowsLive.Id.Client;

We recommend that you declare the variables to contain instances of the IdentityManager and Identity objects at the class level so that all your code can access them. From the sample application:

    public partial class MainWindow : Form
    {
        IdentityManager oIDMgr;
        Identity oID;
Bb676892.note(en-us,MSDN.10).gifImportant:
If the variable that contains the IdentityManager instance falls out of scope, all Identity objects created from it will be invalid.

Before you can authenticate a user, you must initialize the variables that contain the IdentityManager and Identity object instances. We recommend that you do this in your class constructor. From the sample application:

public MainWindow()
{
    InitializeComponent();
    //Try initializing the global instance of IdentityManager.
    try
    {
        oIDMgr = IdentityManager.CreateInstance("Tailspin Toys;someone@tailspintoys.com;Tailspin Toys Application", "Windows Live ID Client Sample");
    }
    catch (WLLogOnException wlex)
    {
        //Check to see if FlowUrl is defined.
        if (wlex.FlowUrl != null)
        {
            //If FlowUrl is defined, direct user to the web page to correct the error.
            MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error");
        }
        else
        {
            //If FlowUrl is not defined, simply display the ErrorString.
            MessageBox.Show(wlex.ErrorString);
        }
    }

    //Check the config file to see if a default user is defined.
    defaultUserName = ConfigurationManager.AppSettings["defaultUserName"];
    if (!String.IsNullOrEmpty(defaultUserName))
    {
        TrySilentSignIn();
    }
    else
    {
        //If no default user is defined, try instantiating the global Identity object instance from scratch.
        try
        {
            oID = oIDMgr.CreateIdentity();
        }
        catch (WLLogOnException wlex)
        {
            //Check to see if FlowUrl is defined.
            if (wlex.FlowUrl != null)
            {
                //If FlowUrl is defined, direct user to the web page to correct the error.
                MessageBox.Show(wlex.ErrorString + "Please go to " + wlex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error");
            }
            else
            {
                //If FlowUrl is not defined, simply display the ErrorString.
                MessageBox.Show(wlex.ErrorString);
            }
        }
    }
    UpdateDisplay();
}

This code executes when the MainWindow class is created. First, it calls the CreateInstance method to initialize the variable that contains the global instance of IdentityManager.

Bb676892.note(en-us,MSDN.10).gifNote:
When calling the CreateInstance method, the application name that you specify in the second parameter will appear at the top of the Sign In dialog box, if applicable, as in this illustration.
Windows Live ID Sign In dialog box
Bb676892.9f9a6caf-1e88-413e-a988-a6656a700e5d(en-us,MSDN.10).gif

Next, the code tries to automatically sign in the default user, if any. For more information about automatic sign-in, see Implementing Automatic Sign-in.

If there is no default user, the CreateIdentity method is called to initialize the variable that contains the Identity.

See Also

Tasks

Running the Windows Live ID Sample Application

Concepts

Implementing Automatic Sign-in
Implementing Personalization
Code Samples for Windows Live ID for Client Applications

Page view tracker