Export (0) Print
Expand All

Signing In to Messenger (JavaScript Library)

Ff749551.note(en-us,MSDN.10).gifNote:
Current information about Live Connect is now available in the Windows Live Developer Center. The information in the following sections is provided for legacy purposes only.

This topic describes how to sign a user in the Windows Live Messenger Connect and enable user access to Messenger-specific features, such as user presence, chat, and conversations. It contains the following sections:

  • Adding the Messenger.SignIn offer.
  • Getting an instance of the Messenger Context.
  • Signing the user in to Messenger.
  • Getting the instance of Microsoft.Live.Messenger.User for the signed-in user.

To add the Messenger.SignIn offer to the list of offers in the scope property of the app tag, use the following syntax.

<wl:app
    channel-url="http://example.com/channel.html"
    callback-url="http://example.com/OAuthWrapCallback.axd"
    client-id="0000000008028888"
    scope="WL_Contacts.View,Messenger.SignIn"
    onload="appOnload">
</wl:app>

To see the list of offers, go to Messenger Connect Scopes.

Note that the usage differs if you are initializing the application programmatically. For more information, see Configuring Your Application by Using the App Tag.

You can get an instance of Microsoft.Live.App.messengerContext after the user is authenticated. Verify that authentication is successful before getting the Messenger Context.

The code example in this section demonstrates how to authenticate the user and get the messenger context.

First, the event handler for applicationLoadCompleted returns an instance of Auth.

The event handler for signInCompleted uses the Auth instance to check whether authentication is successful, and then gets the Microsoft.Live.App.messengerContext instance.

function appOnload(evtArgs) {
    Microsoft.Live.Core.Namespace.using("wl:Microsoft.Live");
    Microsoft.Live.Core.Namespace.using("msgr:Microsoft.Live.Messenger");
    appContext = evtArgs.get_dataContext();
    auth = wl.App.get_auth();
}
function onSignin() {
    if (auth) {
        if (auth.get_state() == Microsoft.Live.AuthState.failed) {
            Sys.Debug.trace("Authentication failed.");
            return;
        }
        else if (auth.get_state() == Microsoft.Live.AuthState.authenticated) {
            Sys.Debug.trace("Authentication succeeded.");
            dataContext = wl.App.get_dataContext();
            messengerContext = wl.App.get_messengerContext();
        }
    }
}

To sign the authenticated user in to Messenger, call signIn from the messengerContext instance, as shown in the following code example.

var status = Microsoft.Live.Messenger.PresenceStatus.online;
messengerContext.signIn(status, onSignedIn);

By using the optional parameters for signIn, the user's status can be set at sign-in time, and an event handler function for Microsoft.Live.IMessengerContext.OnSignedIn can be defined. You can use this function to determine whether the sign-in operation is successful. The following example shows a sign-in operation.

function onMsgrSignin(evtArgs) {
    if (evtArgs != Microsoft.Live.Messenger.SignInResultCode.success)
    {
        Sys.Debug.trace("Messenger sign in failed.");
    }
    else
    {
        Sys.Debug.trace("Messenger sign in succeeded.");
    }
}

For more information, see Microsoft.Live.IMessengerContext.

To sign the user out, call Microsoft.Live.IMessengerContext.SignOut on the messengerContext instance, as shown in the following example.

messengerContext.signOut(onSignedOut);

The User object (Microsoft.Live.Messenger.User class) represents the user that is signed in currently. You can access the User object from the messengerContext instance. The User object provides access to the user's Contact List, so that a user can add or remove contacts, monitor the presence status of contacts, send and receive messages, and receive invitations from other users.

The following code example shows how to get the user instance.

user = messengerContext.get_user();

The User object is created when an authenticated user is signed in to the Messenger service by using a Windows Live ID. Users can sign in only by using the Sign In Control. The User object exists only for the life of the web page. A single instance can be accessed after a user is signed in. The User object is disposed when the user navigates away from the page, refreshes the page, or closes the browser.

When you declare the User object, you should create an event handler that detects when it becomes available. The following procedure demonstrates the recommended method to access the User object.

  1. Add an onsignin attribute to the Sign In Control with the specified handler.
    <wl:signin
        onsignin="onSignIn">
    </wl:signin>
    
  2. Define a handler that detects when authentication is successful. When the Messenger Context property is changed, call the specified delegate function.
    function onSignIn() {
       if (auth) {
            if (auth.get_state() == Microsoft.Live.AuthState.failed) {
                alert("Authentication failed.");
                return;
            }
            else if (auth.get_state() == Microsoft.Live.AuthState.authenticated) {
                dataContext = wl.App.get_dataContext();
               messengerContext = wl.App.get_messengerContext();
               //Detects 
               messengerContext.add_propertyChanged(onPropertyChanged);
            }
        }
    }
    
  3. Define an add_propertyChanged handler that detects when the User object is instantiated, and then executes your code.
    function onPropertyChanged(sender, args)
    {
        if(args.get_propertyName() == 'user') 
        {
            //Execute your code by accessing the User object here.
        }
    }
    
Show:
© 2014 Microsoft