Last active
August 29, 2015 14:13
-
-
Save clientbala/71456445b6578003663c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; | |
var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; | |
string TenantIdClaimType = "http://schemas.microsoft.com/identity/claims/tenantid"; | |
string tenantId = ClaimsPrincipal.Current.FindFirst(TenantIdClaimType).Value; | |
string upn = HttpContext.Current.User.Identity.Name; | |
Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext ac = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(string.Format("https://login.windows.net/{0}", tenantId)); | |
ClientCredential cc = new ClientCredential(SettingsHelper.ClientId, SettingsHelper.AppKey); | |
AuthenticationResult result = ac.AcquireToken("https://graph.windows.net", cc); | |
HttpClient client = new HttpClient(); | |
string requestUrl = String.Format( | |
CultureInfo.InvariantCulture, | |
"https://graph.windows.net/{0}/users/{1}?api-version=2013-11-08", | |
HttpUtility.UrlEncode(tenantId), HttpUtility.UrlEncode(upn)); | |
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl); | |
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); | |
HttpResponseMessage response = client.SendAsync(request).Result; | |
User user = null; // Type "User" is a custom class with the userprofile properties | |
if (response.IsSuccessStatusCode) | |
{ | |
string responseString = response.Content.ReadAsStringAsync().Result; | |
user = JsonConvert.DeserializeObject<User>(responseString); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment