Last active
July 6, 2016 00:02
-
-
Save nbarbettini/6e1c21367d88dd6ce538 to your computer and use it in GitHub Desktop.
C# demo with HttpWebRequest connecting to Stormpath API
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
// NOTE: This code is deprecated in favor of the Stormpath .NET SDK | |
// see https://docs.stormpath.com/csharp/product-guide/latest/quickstart.html | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Net; | |
using System.Text; | |
using Newtonsoft.Json; | |
// Demo of using WebRequest to do raw (authenticated) API requests against Stormpath. | |
// It's much easier with the SDK library :) https://github.com/stormpath/stormpath-sdk-dotnet | |
namespace HttpWebRequestDemo | |
{ | |
class Program | |
{ | |
// Get these from your Stormpath admin console | |
private static string API_KEY_ID = "Your_Stormpath_API_key_ID"; | |
private static string API_KEY_SECRET = "Your_Stormpath_API_key_secret"; | |
static void Main(string[] args) | |
{ | |
// First, we need to get the current tenant's actual URL | |
string tenantUrl = null; | |
var getCurrentTenantRequest = BuildRequest("GET", "https://api.stormpath.com/v1/tenants/current"); | |
try | |
{ | |
using (var response = getCurrentTenantRequest.GetResponse()) | |
{ | |
tenantUrl = response.Headers["Location"]; | |
} | |
} | |
catch (WebException wex) | |
{ | |
Console.WriteLine("Request failed. {0}", wex.Message); | |
throw; | |
} | |
// Now that we have the real tenant URL, get the tenant info | |
string tenantData = null; | |
var getTenantInfoRequest = BuildRequest("GET", tenantUrl); | |
try | |
{ | |
using (var response = getTenantInfoRequest.GetResponse()) | |
using (var responseStream = response.GetResponseStream()) | |
using (var reader = new StreamReader(responseStream)) | |
{ | |
tenantData = reader.ReadToEnd(); | |
} | |
} | |
catch (WebException wex) | |
{ | |
Console.WriteLine("Request failed. {0}", wex.Message); | |
throw; | |
} | |
// Use JSON.NET to parse the data and get the tenant name | |
var parsedData = JsonConvert.DeserializeObject<Dictionary<string, object>>(tenantData); | |
Console.WriteLine("Current tenant is: {0}", parsedData["name"]); | |
// Wait for user input | |
Console.ReadKey(false); | |
} | |
private static HttpWebRequest BuildRequest(string method, string uri) | |
{ | |
// Set up request | |
var request = WebRequest.Create(uri) as HttpWebRequest; | |
request.UserAgent = "dotnet/csharp web-request"; | |
request.Method = method; | |
request.ContentType = "application/json"; | |
// Important, otherwise the WebRequest will try to auto-follow | |
// 302 redirects without applying the authorization header to the | |
// subsequent requests. | |
request.AllowAutoRedirect = false; | |
// Construct HTTP Basic authorization header | |
var authPayload = string.Format("{0}:{1}", API_KEY_ID, API_KEY_SECRET); | |
var authPayloadEncoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(authPayload)); | |
request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + authPayloadEncoded); | |
return request; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment