To obtain user information using an authentication token from .Net Backend Azure Mobile Service:
- Install your mobile service using authentication for different providers by visiting this link - I did it for Microsoft, Google and Facebook.
- Add a new controller to the mobile service and add the following code:
public class UserInfoController: ApiController {public ApiServices Services {get; set; }
[AuthorizeLevel(AuthorizationLevel.User)]
public async Task<JObject> GetUserInfo()
{
ServiceUser user = this.User as ServiceUser;
if (user == null)
{
throw new InvalidOperationException("This can only be called by authenticated clients");
}
var identities = await user.GetIdentitiesAsync();
var result = new JObject();
var fb = identities.OfType<FacebookCredentials>().FirstOrDefault();
if (fb != null)
{
var accessToken = fb.AccessToken;
result.Add("facebook", await GetProviderInfo("https://graph.facebook.com/me?access_token=" + accessToken));
}
var ms = identities.OfType<MicrosoftAccountCredentials>().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));
}
var google = identities.OfType<GoogleCredentials>().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<JToken> GetProviderInfo(string url)
{
var c = new HttpClient();
var resp = await c.GetAsync(url);
resp.EnsureSuccessStatusCode();
return JToken.Parse(await resp.Content.ReadAsStringAsync());
}
}
. , , (Microsoft, Facebook Google), API .
- GetUserInfo :
var user = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount);
var userInfo = await App.MobileService.InvokeApiAsync("userInfo", HttpMethod.Get, null);
, , .., , , MS_FacebookScope MS_MicrosoftScope . .