1

After a lot of digging around I've got my WPF application signing users in via Azure Mobile Service. My Mobile Service is connected to an Azure Active Directory that I have set up. However, when I log the user in with MobileServiceClient.LoginAsync(...) the MobileServiceUser UserId is in an unreadable hash it seems. For example it looks like: "Aad:X3pvh6mmo2AgTyHdCA3Hwn6uBy91rXXXXXXXXXX". What exactly is this?

I'd like to grab the user's display name to use but I can't figure out how.

Ebsan
  • 758
  • 1
  • 10
  • 33

2 Answers2

3

That is the userID of Azure Active Directory. You need to create a service to expose your AAD info through a service and retrieve the additional information using the access token you get from your user.

First:

    ServiceUser user = this.User as ServiceUser;
    var identities = await user.GetIdentitiesAsync();
    var aad = identities.OfType<AzureActiveDirectoryCredentials>().FirstOrDefault();
    var aadAccessToken = aad.AccessToken;
    var aadObjectId = aad.ObjectId;

This will give you the access token and objectID , then you need to query the information through AAD graphy API. https://msdn.microsoft.com/library/azure/dn151678.aspx Look at the sample request part. You should provide the query with the access token you got and objectId.

beast
  • 183
  • 7
0

Here is an alternative approach, after reading http://justazure.com/azure-active-directory-part-2-building-web-applications-azure-ad/ scroll to the section on Identity in .Net it talks how claims are a standard part of the framework. So once you get the credentials object like provided by @beast

var aad = identities.OfType<AzureActiveDirectoryCredentials>().FirstOrDefault();

You can actually grab a dictionary with the various properties. Examples of some the properties can be found at https://msdn.microsoft.com/en-us/library/system.identitymodel.claims.claimtypes(v=vs.110).aspx

So from there you can do the following:

if (aad != null)
{
    var d = aad.Claims;
    var email = d[ClaimTypes.Email];
}

I did this to pull the user id which was cross referenced in a table. FYI I am using App Service, but I believe the credentials object is the same in Mobile Service

Ade
  • 631
  • 5
  • 8