Get user information like name, email id, etc. From an authentication token in the .NET Backend Azure Mobile Service

I am using Azure Mobile Service to add authentication to a Windows Store app. After this article, from the documentation for mobile services, I can get UserId as well MobileServiceAuthenticationToken(for both Google and Microsoft account)

My question is: how to get user information, for example, name, email Idetc., using MobileServiceAuthenticationTokenin a mobile service .NET. I looked at various articles explaining how to do this in JavascriptBackend Support , but I couldn’t find anything properly in the mobile service C# + .NET.

Any pointers appreciated.

thanks

+4
source share
2 answers

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()
    {
        //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<FacebookCredentials>().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<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));
        }

        //Check if the user has logged in using Google as Identity provider
        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 .

  1. GetUserInfo :
var user = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount);                  
var userInfo = await App.MobileService.InvokeApiAsync("userInfo", HttpMethod.Get, null);

, , .., , , MS_FacebookScope MS_MicrosoftScope . .

+3

Facebook, Google , e-mail . access token, .

API- Facebook name, email MobileServiceAuthenticationToken.

API Facebook: https://facebookgraphapi.codeplex.com/

// MobileServiceAuthenticationToken <- your token
var facebook = new FacebookGraphAPI(MobileServiceAuthenticationToken);

// Get user profile data 
var user = facebook.GetObject("me", null);
Console.WriteLine(user["name"]);
+7

Source: https://habr.com/ru/post/1605879/


All Articles