Skip to content

Instantly share code, notes, and snippets.

@sandrinodimattia
Created August 17, 2015 22:36
Show Gist options
  • Save sandrinodimattia/e6a0b5ee722503fba3a1 to your computer and use it in GitHub Desktop.
Save sandrinodimattia/e6a0b5ee722503fba3a1 to your computer and use it in GitHub Desktop.
Auth0 impersonation for SharePoint
var domain = "YOUR_DOMAIN.auth0.com";
var globalClientId = "YOUR_GLOBAL_CLIENT_ID";
var globalClientSecret = "YOUR_GLOBAL_CLIENT_SECRET";
var impersonatorId = "USER_ID_OF_THE_IMPERSONATOR";
var targetUserId = "USER_ID_OF_THE_USER_YOU_ARE_TRYING_TO_IMPERSONATE";
var targetClientId = "CLIENT_ID_OF_THE_APPLICATION_YOU_WANT_TO_LOGIN_TO";
var callbackUrl = "SHAREPOINT_URL/_trust/";
var impersonationLinkLifetime = 30; // seconds
// Authenticate.
var client = new HttpClient();
var message = client.PostAsync(String.Format("https://{0}/oauth/token", domain),
new StringContent(JsonConvert.SerializeObject(new { client_id = globalClientId, client_secret = globalClientSecret, grant_type = "client_credentials" }), Encoding.UTF8, "application/json")).Result;
var tokenString = JsonConvert.DeserializeObject<Dictionary<string,string>>(message.Content.ReadAsStringAsync().Result);
var token = tokenString["access_token"];
// Get the impersonation url.
var impersonationBody = new
{
ttl = impersonationLinkLifetime,
protocol = "wsfed",
impersonator_id = impersonatorId,
client_id = targetClientId,
additionalParameters = new {
callback_url = callbackUrl,
wctx = "/"
}
};
var request = new HttpRequestMessage() { RequestUri = new Uri(String.Format("https://{0}/users/{1}/impersonate", domain, targetUserId)), Method = HttpMethod.Post };
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
request.Content = new StringContent(JsonConvert.SerializeObject(impersonationBody), Encoding.UTF8, "application/json");
var url = client.SendAsync(request).Result.Content.ReadAsStringAsync().Result;
Console.WriteLine(url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment