Signing users in to OneDrive (Windows Runtime apps using C#/VB and XAML)

Let users sign in to their Microsoft OneDrive from your Windows Runtime apps using C#/VB and XAML.

In this article
Prerequisites
Add a reference to the Live SDK
Associate your app with the Store
Specify consent page settings
Sign the user in
Summary

The user must be signed in to his or her Microsoft account for your app to work with the user's info on OneDrive. You help the user do this by adding sign-in functionality to your app in the form of a button or a hyperlink.

Note

New OneDrive SDKs are available and hosted on GitHub for multiple platforms, including the OneDrive SDK for iOS, C#, and Python. For a complete list, see SDKs for OneDrive integration.

Prerequisites

We assume that:

  • You are familiar with C# or Silverlight, and can develop Windows Runtime apps.

  • If you're building a Windows Phone Store app or Windows Store app, use Microsoft Visual Studio2013 Update2, which includes the Windows Phone 8.1 SDK. If you're building a Windows Phone 8 app, use Microsoft Visual Studio2012, which includes the Windows Phone 8 SDK.

  • You must have an app to which you want to add sign-in functionality.

  • The Live SDK must be installed on your machine.

Add a reference to the Live SDK

After you have installed the Live SDK from the download site, you must add a reference to it in your app.

Note

Referencing the Live SDK in this way gives you the benefit of using single sign-on. We recommend that you take advantage of the Microsoft account single sign-on feature. Note that if you use single sign-on, your app can't sign the user out. For more info about single sign-on, see Single sign-on for apps and websites.

  1. In Visual Studio2013 Update2, open your app's project.

  2. In Solution Explorer, right-click References > Add Reference.

  3. Click Windows 8.1 or Windows Phone 8.1 > Extensions and select Live SDK.

  4. Click OK.

  5. Add the corresponding using statements to your code, like this.

    using Microsoft.Live;
    

Note

Windows Phone Silverlight apps only: If your project is a Windows Phone Microsoft Silverlight 8 app and you are using Visual Studio2012, or if you don't want to enable single sign-on in your Windows Phone Silverlight 8.1 app, you may also add a reference by right-clicking References > Add Reference, and then selecting Microsoft.Live and Microsoft.Live.Controls under the Assemblies > Extensions tab.

Note

By referencing Microsoft.Live and Microsoft.Live.Controls DLL's, you will have to make only minimal code changes when you retarget your app from Windows Phone 8 to Windows Phone 8.1. However, you will not be able to use single sign-on.

Referencing the Live SDK from NuGet

  1. In Microsoft Visual Studio, open your app's project.

  2. In Solution Explorer, right-click References > Manage NuGet Packages.

  3. Select nuget.org in the Manage NuGet packages window, and search for "Live SDK".

  4. Install the Live SDK version 5.6.

Note

Windows Phone Silverlight apps only: If you reference the Live SDK from NuGet, you are referencing Microsoft.Live.dll and Microsoft.Live.Controls.dll and you won't be able to use the Microsoft account single sign-on feature. If you intend to use single sign-on, install the Live SDK from the download site.

Associate your app with the Store

  1. In Visual Studio, open your app's project.

  2. Right-click the project, click Store, and then click Associate App with the Store.

If you are creating a Windows Phone Silverlight 8.1 app, and you want to use the single sign-on feature, Store association is not supported in Visual Studio. To associate your app with the Store:

  1. Create a Windows Phone 8.1 app that doesn't use Silverlight.

  2. Associate the app with the Store by following the steps in the previous procedure.

  3. Open the Package.manifest file, copy the Identity node, and paste it to replace the Identity node of the Package.manifest file in your Silverlight 8.1 app with that value.

When your app invokes the API to ask for the user's consent, the Allow access page (also called the consent page) is displayed. The consent page shows the types of info that your app wants to access. To customize the items on the consent page, go to your Microsoft account Developer Center and specify these items.

Consent page

App management site setting

Your app's logo

Basic Information page, Application logo image

Your app's domain name, which appears in several places in the consent page

API Settings page, Root Domain box

Your app's name, which appears in several places in the consent page

Basic Information page, Application name box

Your app's terms-of-use hyperlink

Basic Information page, Terms of service URL box

Your app's privacy statement hyperlink

Basic Information page, Privacy URL box

Note

If your app is a Windows Phone Silverlight 8 app, you must specify that your app is a mobile client app. To do so, in API Settings, select Yes under Mobile or desktop client app.

Note

If the Terms of service URL and Privacy URL settings are not specified, the text APPLICATION_NAMEterms and privacy statement will not appear in the consent page.

Sign the user in

Windows Phone Store apps can use the sign-in control provided by the Live SDK. You set the LiveConnectSession.Scopes property to indicate the scopes and permissions required.

  1. Create a button in your MainPage.xaml file to prompt users to sign in, if they are not signed in.

    <Button x:Name="connectButton" Visibility="Collapsed" HorizontalAlignment="Center" 
        Content="Connect with Microsoft Account" 
    Width="320" Click="connectButton_Click"></Button>
    
  2. Add the button-click event handler to sign the user in. Create a Microsoft.Live.LiveAuthClient class and call one of the LiveAuthClient.InitializeAsync methods to initialize the Live SDK. Then call one of the LiveAuthClient.LoginAsync methods with the wl.signin and wl.skydrive scopes to enable single sign-in and allow the user to access OneDrive.

    private async void connectButton_Click(object sender, RoutedEventArgs e)
    {
        bool connected = false;
        try
        {
            var authClient = new LiveAuthClient();
            LiveLoginResult result = await authClient.LoginAsync(new string[] {"wl.signin", "wl.skydrive"});
    
            if (result.Status == LiveConnectSessionStatus.Connected)
            {
                connected = true;
                var connectClient = new LiveConnectClient(result.Session);
                var meResult = await connectClient.GetAsync("me");
                dynamic meData = meResult.Result;
                updateUI(meData);
            }
        }
        catch (LiveAuthException ex)
        {
            // Display an error message.
        }
        catch (LiveConnectException ex)
        {
            // Display an error message.
        }
    
        // Turn off the display of the connection button in the UI.
        connectButton.Visibility = connected ? Visibility.Collapsed : Visibility.Visible;
    }
    

Summary

You now know how to sign users in to OneDrive from your Windows Runtime app. Next, you can learn how to let users access their files and folders on OneDrive from your app. To do this, see File and folder properties (XAML).