Connecting to Live Mesh with the Live ID Client SDK
Microsoft’s Live Framework team recently released the April CTP update of the Live Framework SDK and Tools. This update has been released publicly on the Download Center, so you can grab it and get started right away.
If you want to run the Live Mesh Client beta and the Live Framework Client side by side, you’ll want to make sure and follow the instructions prior to installing the April CTP Update. Note that you will actually be developing against the Live Framework Client and cannot currently target the Live Mesh Client. I assume these will merge at some point, and you’ll be able to play in both the live runtime as well as the development runtime, but for now, you’re stuck in the developer playground, so we’ll technically be connecting to the Live Framework Client (not the Live Mesh Client) for now.
I’ve been working on a Live Mesh demo rich client application, and I figured I may as well use the Live ID Client SDK rather than worry about secure credential storage. This proved to be a bit challenging for a number of reasons, so I thought I’d post my working formula for those that might follow this dark path.
It turns out that the Live ID Client SDK only supports the x86 platform, so the first thing you’ll run across is the installer blows up while installing it on the x64 platform. It turns out that it still manages to drop the DLLs you’ll need in %CommonProgramFiles(x86)%\Microsoft Shared\WLIDClient.
Create a WPF Application project and add references to Microsoft.LiveFX.Client, Microsoft.LiveFX.ResourceModel, and Microsoft.WindowsLive.Id.Client (found in the aforementioned path as well as %ProgramFiles(x86)%\Microsoft SDKs\Live Framework\v0.91\API Toolkits\.Net Library). Since the Live ID Client SDK will only work for x86, you’ll need to change your Project Settings for the project that will be utilizing the SDK (highlighted below):
While you’re here, add a DefaultUserName setting to store the user’s Live ID name. We’ll use this later for automatic sign-in:
Now that all that’s out of the way, we can get on with writing code. Throw a button on the default layout, rename it, and add a click handler. Inside the click handler, add the following code:
IdentityManager manager = IdentityManager.CreateInstance(
"Sean Timm;thread@gmail.com;Live Mesh Demo",
"Live Mesh Demo");
string currentUser = Settings.Default.DefaultUserName;
Identity identity;
if(String.IsNullOrEmpty(currentUser))
{
identity = manager.CreateIdentity();
}
else
{
identity = manager.CreateIdentity(currentUser);
}
This code creates the Identity instance that will provide your authentication token. IdentityManager.CreateInstance takes two parameters: an application ID and an application name. The application ID is composed of three parts: Company Name;Email Address;Application Name. In practice, I’ve found I can set this to whatever I want, but the documentation and examples call it out this way, so you’re probably best to stick with this format. The application name passed in as the second parameter is used during display of the sign in dialog.
If a DefaultUserName has previously been saved, it will instantiate the instance using the value to take advantage of any stored credentials for automatic sign in. Now add the logic to do the actual sign in:
if(identity.SavedCredentials == CredentialType.UserNameAndPassword)
{
identity.Authenticate(AuthenticationType.Silent);
}
if(!identity.IsAuthenticated)
{
identity.Authenticate();
Settings.Default.DefaultUserName = identity.UserName;
Settings.Default.Save();
}
This logic checks if both the username and password have been stored from a previous login (based on the DefaultUserName passed into the instance). If they are available, it authenticates silently. Otherwise, you’ll get the standard sign in dialog:
Notice the application name above the blue guy. That’s the value you passed in during creation of the IdentityManager. Once you log in, the username is saved for future automatic sign-in (if you checked Remember my Password).
To connect to the LiveOperatingEnvironment in the Live Framework Client, you’ll need an authentication token. You can retrieve an authentication token from your authenticated identity via the following:
string token = identity.GetTicket("user-ctp.windows.net", "MBI_SSL", true);
There are a number of other ways to obtain a token. If you had been using credentials directly (rather than through the Live ID Client SDK), you could have done so via the following:
string token = new System.Net.NetworkCredential("myliveid", "mypassword").GetWindowsLiveAuthenticationToken();
Now that you have a token, it’s time to connect to the LiveOperatingEnvironment:
var uri = new Uri("https://user-ctp.windows.net");
var loe = new LiveOperatingEnvironment();
loe.Connect(token,
AuthenticationTokenType.UserToken,
uri,
new LiveItemAccessOptions(true));
Once this returns, you’re good to go. It will automatically connect to either the local or cloud environment depending on what’s available, and you can start playing around with the Mesh!
There are a number of asynchronous call alternatives that you can use when it makes sense. I opted to throw it all in a thread to avoid an unresponsive UI. Here’s the completed class:
using System;using System.Threading;using System.Windows;using MeshDemo.Properties;using Microsoft.LiveFX.Client;using Microsoft.LiveFX.ResourceModel;using Microsoft.WindowsLive.Id.Client;namespace MeshDemo{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{readonly LiveOperatingEnvironment loe = new LiveOperatingEnvironment();public MainWindow(){InitializeComponent();}private void signInButton_Click(object sender, RoutedEventArgs e){signInButton.IsEnabled = false;var thread = new Thread(() =>{IdentityManager manager = IdentityManager.CreateInstance("Sean Timm;thread@gmail.com;Live Mesh Demo","Live Mesh Demo");string currentUser = Settings.Default.DefaultUserName;Identity identity;if(String.IsNullOrEmpty(currentUser)){identity = manager.CreateIdentity();}else{identity = manager.CreateIdentity(currentUser);}if(identity.SavedCredentials ==CredentialType.UserNameAndPassword){identity.Authenticate(AuthenticationType.Silent);}if(!identity.IsAuthenticated){identity.Authenticate();Settings.Default.DefaultUserName = identity.UserName;Settings.Default.Save();}string errorMessage = String.Empty;bool isConnected = false;if(identity.IsAuthenticated){string token = identity.GetTicket("user-ctp.windows.net", "MBI_SSL", true);var uri = new Uri("https://user-ctp.windows.net");try{loe.Connect(token, AuthenticationTokenType.UserToken, uri,new LiveItemAccessOptions(true));Action signInComplete = OnSignInComplete;Dispatcher.BeginInvoke(signInComplete);isConnected = true;}catch (Exception ex){errorMessage = ex.Message;}}else{errorMessage = "Failed to authenticate";}if(!isConnected){Action<string> signInFailed = OnSignInFailed;Dispatcher.BeginInvoke(signInFailed, errorMessage);}});thread.SetApartmentState(ApartmentState.STA);thread.Start();}private void OnSignInComplete(){signInButton.Visibility = Visibility.Collapsed;}private void OnSignInFailed(string message){signInButton.IsEnabled = true;if(String.IsNullOrEmpty(message)){MessageBox.Show("Sign in failed for some frustrating reason.");}else{MessageBox.Show(String.Format("Sign in failed for this frustrating reason: {0}", message));}}}}
Notice I explicitly set the thread to single-threaded apartment. The Live ID Client SDK COM dependencies require this.
Now that you can connect, it’s time to go forth and make a Mesh of things!






Hello, your example gets WebException:
The remote server returned an error: (404) Not Found.
I used Live Framework SDK CTP April 2009 and Windows Live ID SDK. Can you point me where I’m wrong ?
I just verified that this is still working. Maybe something was temporarily screwed up at https://user-ctp.windows.net? You might try again. Is it failing during loe.Connect?
Maybe problem in IP location ?
Yes, the exception is rised in Connect:
string serverUrl = “https://user-ctp.windows.net”;
Uri uri = new Uri(serverUrl);
LiveItemAccessOptions liao = new LiveItemAccessOptions(true);
token = identity.GetTicket(“user-ctp.windows.net”, “MBI_SSL”, true);
env.Connect(token, AuthenticationTokenType.UserToken, uri, liao);
where identity is from Windows Live ID Client