简体   繁体   中英

Azure AD B2C user info Xamarin

Hello I am trying to get user info like name and address in an Xamarin app using Azure AD B2C for authentication.

So far I've gotten the authentication working fine

public async Task<MobileServiceUser> LoginAsync(MobileServiceClient client, MobileServiceAuthenticationProvider provider)
{
  try
  {
    //login and save user status
    var user = await client.LoginAsync(Forms.Context, provider);

    Settings.AuthToken = user?.MobileServiceAuthenticationToken ?? string.Empty;
    Settings.UserId = user?.UserId ?? string.Empty;
    return user;
  }
  catch (Exception e)
  {
  }

  return null;
}

However I would like to know how to get the user's name and birthday. I haven't been able to find a clear course of action for that.

You do not explicitly get this information using the MobileService SDK. Check out the complete documentation about App Service Authentication/Authorization here .

You will reach the point where it mentions:

Your application can also obtain additional user details through an HTTP GET on the /.auth/me endpoint of your application. A valid token that's included with the request will return a JSON payload with details about the provider that's being used, the underlying provider token, and some other user information. The Mobile Apps server SDKs provide helper methods to work with this data.

So, in your Xamarin, after the user is successfully authentication, you have to explicitly make a HTTP GET request to /.auth/me and parse the result to get all information about the logged-in user.

Not sure how to do this in Xamarin, but here is how to do it in C# UWP (Universal Windows Platform):

                var url = App.MobileService.MobileAppUri + "/.auth/me";
                var clent = new Windows.Web.Http.HttpClient();
                clent.DefaultRequestHeaders.Add("X-ZUMO-AUTH", this.user.MobileServiceAuthenticationToken);
                var userData = await clent.GetAsync(new Uri(url));

at the of this code execution, the userData varibale will be a JSON srting with all user's claims.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM