Skip to content

Instantly share code, notes, and snippets.

@clientbala
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clientbala/71456445b6578003663c to your computer and use it in GitHub Desktop.
Save clientbala/71456445b6578003663c to your computer and use it in GitHub Desktop.
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