I am using Azure Mobile Service to add authentication to my Windows Store app. Following this article from Mobile Services documentation I am able to get the UserId as well
For getting user info using authentication token from .Net Backend Azure Mobile Service:
public class UserInfoController : ApiController { public ApiServices Services { get; set; }
[AuthorizeLevel(AuthorizationLevel.User)]
public async Task GetUserInfo()
{
//Get the current logged in user
ServiceUser user = this.User as ServiceUser;
if (user == null)
{
throw new InvalidOperationException("This can only be called by authenticated clients");
}
//Get Identity Information for the current logged in user
var identities = await user.GetIdentitiesAsync();
var result = new JObject();
//Check if the user has logged in using Facebook as Identity provider
var fb = identities.OfType().FirstOrDefault();
if (fb != null)
{
var accessToken = fb.AccessToken;
result.Add("facebook", await GetProviderInfo("https://graph.facebook.com/me?access_token=" + accessToken));
}
//Check if the user has logged in using Microsoft Identity provider
var ms = identities.OfType().FirstOrDefault();
if (ms != null)
{
var accessToken = ms.AccessToken;
result.Add("microsoft", await GetProviderInfo("https://apis.live.net/v5.0/me/?method=GET&access_token=" + accessToken));
}
//Check if the user has logged in using Google as Identity provider
var google = identities.OfType().FirstOrDefault();
if (google != null)
{
var accessToken = google.AccessToken;
result.Add("google", await GetProviderInfo("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + accessToken));
}
return result;
}
private async Task GetProviderInfo(string url)
{
var c = new HttpClient();
var resp = await c.GetAsync(url);
resp.EnsureSuccessStatusCode();
return JToken.Parse(await resp.Content.ReadAsStringAsync());
}
}
Once done publish the mobile service. The above code gets the identity information for the current logged in user and then depending upon which authentication provider user has chosen (Microsoft, Facebook or Google) it makes a call to that identity provider's user profile API and gets the user information.
var user = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount); var userInfo = await App.MobileService.InvokeApiAsync("userInfo", HttpMethod.Get, null);
However this will bring only some basic user information like name, gender etc. if your application requires more than that you can request additional scopes during the login, by setting the MS_FacebookScope and MS_MicrosoftScope app settings in mobile service Configure Tab in azure portal.
You can get more detailed information on this from this excellent article