Skip to content

Instantly share code, notes, and snippets.

@siacomuzzi
Created June 16, 2014 16:13
Show Gist options
  • Save siacomuzzi/f681cb4703ad3dc1a679 to your computer and use it in GitHub Desktop.
Save siacomuzzi/f681cb4703ad3dc1a679 to your computer and use it in GitHub Desktop.
Call WAMS hosted APIs authenticated with Auth0
class Program
{
private const string Auth0Domain = "{YOU}.auth0.com";
private const string clientId = "{AUTH0_CLIENT_ID}";
private const string targetClientId = "{THE_WAMS_CLIENT_ID_IN_AUTH0}";
private const string id_token = "{USER_ID_TOKEN}";
private const string wamsEndpoint = "https://auth0-tests.azure-mobile.net/tables/people";
static void Main(string[] args)
{
RunAsync().Wait();
}
private static async Task RunAsync()
{
// Get WAMS Token
var wamsToken = string.Empty;
using (var client = new HttpClient { BaseAddress = new Uri(string.Format("https://{0}/", Auth0Domain)) })
{
var parameters = new Dictionary<string, string>()
{
{ "client_id", clientId },
{ "grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer" },
{ "id_token", id_token },
{ "target", targetClientId },
{ "scope", "openid email" }
};
try
{
var response = await client.PostAsync("delegation", new FormUrlEncodedContent(parameters));
response.EnsureSuccessStatusCode();
var delegationResult = await response.Content.ReadAsAsync<DelegationResult>();
wamsToken = delegationResult.IdToken;
Console.WriteLine("WAMS Token: " + wamsToken);
}
catch (HttpRequestException)
{
throw;
}
}
// Call WAMS endpoint
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("x-zumo-auth", wamsToken);
var response = await client.GetAsync(wamsEndpoint);
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine("\r\nWAMS Response: HTTP: {0}, Content: {1}", response.StatusCode, responseString);
}
Console.WriteLine("\r\nDone. Press any key to exit...");
Console.ReadKey();
}
[DataContract]
public class DelegationResult
{
[DataMember(Name="id_token")]
public string IdToken { get; set; }
[DataMember(Name="expires_in")]
public long ExpiresIn { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment