Question

I inherited a Windows 8 application that is written with XAML. So in C# when I make this call

user = await MobileServices.MobileService
                    .LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount);

(This is for Azure Mobile Services)

The user object is ONLY giving me the Token and the MicrosoftAccount:..............

In order to get to authenticate people, I need to be able to see WHO is requesting access...

I looking at articles like below, but I seem to be missing something? Is this javascript in the article something I would have to write in Node.js?

Example article: http://blogs.msdn.com/b/carlosfigueira/archive/2013/12/12/expanded-login-scopes-in-azure-mobile-services.aspx

Was it helpful?

Solution

Currently to be able to get more information about the logged in user, you need to make a second call to the service to retrieve the user info. You don't really need to ask for additional login scopes (the topic of the post you mentioned) to retrieve the user name, since that is given by default for all the providers.

This post should have the code you need to write in the server side (node.js) to get more information about the logged in user. The TL;DR version is given below:

On the server side: add this custom API (I'll call it "userInfo"; set the permission of GET to "user", and all others to admin):

exports.get = function(request, response) {
    var user = request.user;
    user.getIdentities({
        success: function(identities) {
            var accessToken = identities.microsoft.accessToken;
            var url = 'https://apis.live.net/v5.0/me/?method=GET&access_token=' + accessToken;
            var requestCallback = function (err, resp, body) {
                if (err || resp.statusCode !== 200) {
                    console.error('Error sending data to the provider: ', err);
                    response.send(statusCodes.INTERNAL_SERVER_ERROR, body);
                } else {
                    try {
                        var userData = JSON.parse(body);
                        response.send(200, userData);
                    } catch (ex) {
                        console.error('Error parsing response from the provider API: ', ex);
                        response.send(statusCodes.INTERNAL_SERVER_ERROR, ex);
                    }
                }
            }
            var req = require('request');
            var reqOptions = {
                uri: url,
                headers: { Accept: "application/json" }
            };
            req(reqOptions, requestCallback);
        }
    });
}

On the client side, after a successful login, call that API:

user = await MobileServices.MobileService
    .LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount);
var userInfo = await MobileServices.MobileService.InvokeApiAsync(
    "userInfo", HttpMethod.Get, null);

userInfo will contain a JObject with the user information. There is an open feature request to make this better at http://feedback.azure.com/forums/216254-mobile-services/suggestions/5211616-ability-to-intercept-the-login-response.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top