How to get the logged in user details(user email i

2019-05-18 20:07发布

Hi am developing a windows store 8.1 app using C# and xaml

For log in, am authenticating the user with Windows Azure Active directory Single Sign-on using JavaScript back-end.

Once the user is logged in and i have the access token, how to get the logged in user's user email id and Username using the access token in the app?

Anybody please provide me a solution to get the user email using the access token?

1条回答
地球回转人心会变
2楼-- · 2019-05-18 20:35

If you have the access token then that should contain the user id value. For retrieving the e-mail address you have to query the Graph API to get user details. The full documentation on that is here but in short you should make a get request like below, placing the AccessToken in the Authorization header, after "Bearer ".

GET https://graph.windows.net/contoso.onmicrosoft.com/users/Alex@contoso.onmicrosoft.com?api-version=2013-04-05 HTTP/1.1
Authorization: Bearer eyJ0eX ... FWSXfwtQ
Content-Type: application/json

You can use either user principal name or objectId in the address. Better yet, you can use the Azure AD Graph Client nuget package and call their API to get user information.

Uri servicePointUri = new Uri("https://graph.windows.net");
Uri serviceRoot = new Uri(servicePointUri, "contoso.onmicrosoft.com");
ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot, accessToken);
IUser user = activeDirectoryClient.Users
    .Where(user => user.UserPrincipalName.Equals("alex@contoso.onmicrosoft.com"))
    .ExecuteAsync().Result.CurrentPage.ToList().SingleOrDefault();

See here for the full sample.

查看更多
登录 后发表回答