Connect Live SDK in Windows Store Application

Introduction

This article helps you to learn how to connect using Live SDK in a Metro style application. Metro style applications can freely communicate with Live SDK to achieve a single sign-on using a Microsoft account. Metro style applications can connect using Live SDK, JavaScript and managed programing languages like C# and Visual Basic.

Connecting using Live SDK in a Metro style application is very easy. Let's use the following steps to do this.

Step 1 : Install Live SDK

Step 2 : Add a Live SDK reference to the Metro style application

This article assumes you are already familiar with creation of Metro style applications. Create a Metro style application in Visual Studio and add the Live SDK reference from the Solution Explorer.

1.png

Step 3 :
Register your Metro style application in the Windows Live Dashboard. Click here to learn how to register a Metro style application on the Live Dashboard.

Step 4 : Copy the package name from the Live Dashboard and open the Package.appxmanifest file from your application and move to the packaging tab and paste the package name into the package name textbox.

2.png

Step 5 : Add the following two namespaces to your project:

using Microsoft.Live;
using Microsoft.Live.Controls;


Now we need to add a live connect Sign-in control from the Live SDK. In order to use the Sign-in control we need to add the following line of code to the MainPage.xaml file in your application:

<Page
    x:Class="Liveconnection.MainPage"
    IsTabStop="false"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Liveconnection"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:live="using:Microsoft.Live.Controls">
 
  <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <live:SignInButton x:Name="btnSignin"   Scopes="wl.signin
wl.basic"
 SessionChanged="btnSignin_SessionChanged_2" />
    </Grid>
</
Page>

Remember : It is not compulsory to use a live connect Sign-in control; you can also use a button to connect using the Live SDK.

Step 6 : Add the following Live connection code to the MainPage.xaml file:

try
{
    
 
LiveAuthClient liveAuthClient;
     LiveLoginResult liveLoginResult;
     liveAuthClient = 
new LiveAuthClient("{please add your redirect url here}");
     liveLoginResult = 
await liveAuthClient.LoginAsync(new string[] { "wl.signin","wl.basic" });
 
   
 
if (liveLoginResult.Session != null && liveLoginResult.Status ==LiveConnectSessionStatus.Connected)
    {
       
 
LiveConnectClient client = new LiveConnectClient(liveLoginResult.Session);
        LiveOperationResult operationResult = await client.GetAsync("me");
        
dynamic results = operationResult.Result;
        lblWelcome.Text = 
"Welcome " + results.name;
        btnSignin.Content = 
"Sign out"
    }
    
else
       throw new Exception();
}
catch (Exception ex)
{
      lblWelcome.Text = ex.Message;
}


Step 7 : Press F5 to run your project.

3.png

Enter your live credentials and click on sign in:

4.png

Click on yes and continue.

If you receive the following error it means you have not added the package name. Add it and run the apps again.

5.png

Remember : If you are using my project attached to this article then please add your Redirect URL to run the project smoothly. Also add your package name in the Package.appxmanifest file.

Thanks for spending your valuable time to read this article.

Reference: http://msdn.microsoft.com/en-us/library/live/hh826551.aspx


Similar Articles