Export (0) Print
Expand All

How to: Sign In to Windows Live Messenger

The DelegatedAuthControl object is designed to help abstract the functionality that is required to request consent from a user. You can embed the DelegatedAuthControl object on a Web page so that a user can initiate the consent and sign-in process for Windows Live Messenger.

After the user grants consent, the application can sign the user in. When the sign-in process is complete, the application can access the user's contacts, create a conversation, and perform other tasks through the User object. This topic describes the code that is used to host the DelegatedAuthControl object. The following programming tasks create the user sign-in process.

  1. Define an event handler for DelegatedAuthControl.ConsentCompleted event. This event is raised when the user provides consent for the application to use Delegated Authentication. This event handler performs the following tasks.
    • Hide the div element that contains the DelegatedAuthControl object.
    • Pass the consent token to the application server so that it can decrypt the consent token and return the user delegation token.
  2. Define an event handler for the DelegatedAuthIdentity.AuthenticationCompleted event. This event is raised when the user is authenticated by using the delegation token. The event handler performs the following tasks.
    • Instantiate the User object with DelegatedAuthIdentity.
    • Subscribe to the User.SignInCompleted event.
    • Call the User.SignIn method.
  3. Define an event handler for the User.SignInCompleted event. This event is raised when the user signs in to Messenger successfully. You can use this event handler to perform additional tasks, such as displaying the user's contact list.
  4. Create a server-side function that retrieves the delegation token by decrypting the consent token. After receiving the delegation token from the server, the application should create DelegatedAuthIdentity and subscribe to the DelegatedAuthIdentity.AuthenticationCompleted event.
  5. After the host page loads, the application perform the following tasks.
    • Instantiate Microsoft.Live.Messenger.UI.DelegatedAuthControl.
    • Subscribe to the DelegatedAuthControl.ConsentCompleted event.

After a user grants consent to an application, the DelegatedAuthControl object is no longer used to sign in the user. Instead, the following steps are required.

  1. Create DelegatedAuthIdentity with the user's delegation token.
  2. Instantiate the User object with DelegatedAuthIdentity.
  3. Subscribe to the User.SignInCompleted event. This event is raised when a user signs in to Messenger.
  4. Call the User.SignIn method.
Cc298452.note(en-us,MSDN.10).gifNote:
The Privacy.htm and Channel.htm pages are required on your Web site domain to host DelegatedAuthControl. For more information, see Windows Live Messenger Library Development Basics.

  1. In the <head> element of the page, add a <script> element to load the Windows Live Messenger Library.

    <script src="http://www.wlmessenger.net/api/3.7/Loader.js" type="text/javascript" language="javascript"></script>
    
  2. In the body of the Web page, add a <script> element to reference your application's main JavaScript file.

    <script src="SignIn.js" type="text/javascript" language="javascript"></script>
    
  3. Add a DOM element to contain DelegatedAuthControl.

    In this example, it is a <div> with the ID "signinframe".

    <div id="signinframe"> 
            Sign In 
    </div> 
    

  1. Create a new file named SignIn.js and initialize the following variables.

    _user = null;
    _identity = null;
    _delAuthControl = null;
    
  2. Create the onScriptsLoaded function to instantiate an instance of DelegatedAuthControl and subscribe to the consentCompleted event.

    The application verifier token must be generated and inserted with server-side code. In the following example, the token is inserted as a string into the variable appVerifierToken. An application verifier token consists of your application ID and a time stamp, signed with your secret key. For details, see Using Application Verifier Tokens for Delegated Authentication.

    function onScriptsLoaded()
    {
        var hostUrl = window.location.href;
        var index = hostUrl.lastIndexOf("/");
        hostUrl = hostUrl.substring(0, index);
    
        var privUrl = hostUrl + "/Privacy.htm";
        var chanUrl = hostUrl + "/Channel.htm";
                    
        // Use server side code to set your application verifier token here.
        var appVerifierToken = "appid%3d000000006800FBCC%26ts%3d1235706929%26s";
    
        _delAuthControl = new Microsoft.Live.Messenger.UI.DelegatedAuthControl("signinframe", privUrl, chanUrl, "en-US", appVerifierToken);
                    
        // Replace "logo.jpg" with the URL to your application logo.
        _delAuthControl.set_applicationLogoUrl("logo.jpg");
    
        _delAuthControl.add_consentCompleted(consentCompleted);
    } 
    
  3. Create the consentCompleted function to define a delegate that handles the consentCompleted event.

    At this point, the user has provided consent. However, the user is not authenticated or online. The application must use server-side code to decrypt the delegation token by using the consent token.

    function consentCompleted(sender, e) 
    {
       var consentToken = e.get_consentToken();
                
       // An application passes the consent token to its servers. 
       // The consent token decrypts the token and extracts the user delegation token.
       getDelegationToken(consentToken, onGetDelegationToken);
    }
    
  4. Add the code to pass the consent token back to the server to retrieve the delegation token. Implement the getDelegationToken function and call the onGetDelegationToken function when the delegation token is returned.

    function getDelegationToken(consentToken, callback) 
    {
       // TODO: send the consent token to the application server to decrypt
       // and return the user delegation token.
    }
    
  5. Create the onGetDelegationToken function to authenticate the user after the delegation token has been retrieved from the server.

    function onGetDelegationToken(delegationToken) 
    {
       _identity = new DelegatedAuthIdentity(delegationToken);
       _identity.add_authenticationCompleted(onAuthCompleted);
    }
    
  6. Create the onAuthCompleted function to sign the user in to Messenger when authentication is complete. This requires instantiating a User object and calling the signIn function.

    function onAuthCompleted(sender, e) 
    {
       var identity = sender;
               
       if (!e.get_authenticationFailure()) 
       {
          user = new Microsoft.Live.Messenger.User(identity);
          _user.add_signInCompleted(onSignInCompleted);
          _user.signIn();
       }
    }
    
  7. Create the onSignInCompleted function to perform tasks after the user has signed in. In this example, the current address for the user appears in a paragraph HTML element.

    function onSignInCompleted(sender, e) 
    {
       if (e.get_resultCode() === Microsoft.Live.Messenger.SignInResultCode.success) 
       {
           // Display a confirmation message.
           var paragraph = document.createElement('p');
           var confirmMsg = document.createTextNode(_user.get_address().get_address() + ' is now signed in');
           paragraph.appendChild(confirmMsg);
    
           document.body.appendChild(paragraph);
       }
    }
    
  8. Add the following code to the end of your JavaScript file. This code loads the appropriate libraries that are required for DelegatedAuthControl.

    var loader = Microsoft.Live.Core.Loader;
    loader.load(['messenger.core'], onScriptsLoaded);
    

  1. Use server-side code to retrieve the user delegation token that is stored in your application database.

  2. Insert the delegation token into the Web page.

  3. Create the DelegatedAuthIdentity object with the delegation token.

    // Use server-side code to insert the user delegation token here.
    var delegationToken = " EwCgARAnAAAU7KcbIzqHGnbpm1C2zYa2o9TnbAWAAM1RESuqZ7RK7uZdIzukGspQVPjdnMem";
    var delAuthIdentity = new Microsoft.Live.Messeneger.DelegatedAuthIdentity(delegationToken);
    
    _user = new Microsoft.Live.Messenger.User(delAuthIdentity);
    

Note   If the user has provided consent, your application does not need to instantiate DelegatedAuthControl and wait for a consentCompleted event. If your site has stored the delegation token, you can authenticate the user immediately by creating the User object with the DelegatedAuthIdentity object.

The following code example shows the JavaScript to instantiate DelegatedAuthControl and to create delegates to handle the ConsentCompleted and SignInCompleted events.

_user = null;
_identity = null;
_delAuthControl= null;

function onScriptsLoaded()
{
    var hostUrl = window.location.href;
    var index = hostUrl.lastIndexOf("/");
    hostUrl = hostUrl.substring(0, index);

    var privUrl = hostUrl + "/Privacy.htm";
    var chanUrl = hostUrl + "/Channel.htm";
                
    // Use server-side code to set your application verifier token here.
    var appVerifierToken = "appid%3d000000006800FBCC%26ts%3d1235706929%26s";

    _delAuthControl = new Microsoft.Live.Messenger.UI.DelegatedAuthControl("signinframe", privUrl, chanUrl, "en-US", appVerifierToken);
                
    // Replace "logo.jpg" with the URL to your application logo.
    _delAuthControl.set_applicationLogoUrl("logo.jpg");

    _delAuthControl.add_consentCompleted(consentCompleted);
} 

function consentCompleted(sender, e) 
{
   var consentToken = e.get_consentToken();
            
   // An application must pass the consent token to its servers.
   // The consent token decrypts the token and extracts the user's delegation token.
   getDelegationToken(consentToken, onGetDelegationToken);
}

function getDelegationToken(consentToken, callback) 
{
   // TODO: Send the consent token to the application serverto decrypt
   // and return the user delegation token.
}

function onGetDelegationToken(delegationToken) 
{
   _identity = new DelegatedAuthIdentity(delegationToken);
   _identity.add_authenticationCompleted(onAuthCompleted);
}

function onAuthCompleted(sender, e) 
{
   var identity = sender;
           
   if (!e.get_authenticationFailure()) 
   {
       user = new Microsoft.Live.Messenger.User(identity);
       _user.add_signInCompleted(onSignInCompleted);
       _user.signIn();
   }
}

function onSignInCompleted(sender, e) 
{
   if (e.get_resultCode() === Microsoft.Live.Messenger.SignInResultCode.success) 
   {
       // Display a confirmation message.
       var paragraph = document.createElement('p');
       var confirmMsg = document.createTextNode(_user.get_address().get_address() + ' is now signed in');
       paragraph.appendChild(confirmMsg);

       document.body.appendChild(paragraph);
   }
}

var loader = Microsoft.Live.Core.Loader;
loader.load(['messenger.core'], onScriptsLoaded);
Show:
© 2014 Microsoft