Search code examples
c#silverlightwindows-phone-7sdklive

Live SDK - Try to Sign In without SignInButton


Is there any way to login to Live for an App (Silverlight, WP7 can) without having to click on SignIn button.

I want to log me dynamically, for example: when you start the app, I want to log in to me. How to do this without resorting to the button?


Solution

  • I figured out how to do, so I decided to share:

    using System.Windows;
    using Microsoft.Live;
    
    public class LiveLogin
    {
    
        private static readonly string[] scopes = 
            new string[] { 
                "wl.signin", 
                "wl.basic", 
                "wl.calendars", 
                "wl.calendars_update", 
                "wl.contacts_calendars", 
                "wl.events_create" };
    
        private LiveAuthClient authClient;
        private LiveConnectClient liveClient;
    
    
        public LiveLogin()
        {
            this.authClient = new LiveAuthClient("**your client id here**");
            this.authClient.InitializeCompleted += authClient_InitializeCompleted;
            this.authClient.InitializeAsync(scopes);
        }
    
        private void authClient_InitializeCompleted(object sender, LoginCompletedEventArgs e)
        {
            if (e.Status == LiveConnectSessionStatus.Connected)
            {
                this.liveClient = new LiveConnectClient(e.Session);
            }
            else
            {
                this.authClient.LoginCompleted += authClient_LoginCompleted;
                this.authClient.LoginAsync(scopes);
            }
        }
    
        private void authClient_LoginCompleted(object sender, LoginCompletedEventArgs e)
        {
            if (e.Status == LiveConnectSessionStatus.Connected)
            {
                this.liveClient = new LiveConnectClient(e.Session);
                MessageBox.Show("Signed");
            }
            else
            {
                MessageBox.Show("Failed!");
            }
        }
    }