Skip to content

Instantly share code, notes, and snippets.

@aheld
Created November 15, 2012 20:22
Show Gist options
  • Save aheld/4081021 to your computer and use it in GitHub Desktop.
Save aheld/4081021 to your computer and use it in GitHub Desktop.
.net http proxy hack to allow javascript in the browser to authenticate against IIS and have the server call out to antother server
/*
This allows a javascript app to hit a relative url such as
/Proxy/myapp.com/api/rest/2/issues
and then have the server make a call to
http://myapp.com/api/rest/2/issues
the IIS container injects the User obj based on our Active Directory and I turn that into an API token using an encryption scheme.
we do some hacking on a localhost url such as
/Proxy/localhost/api/rest/2/issues
so that calls back to
http://localhost/api/rest/2/issues and is always a GET - this facilitates having local text files that mock server data for dev and UI testing.
/*
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Web;
using System.Web.Routing;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
namespace Proxy.Net
{
public class APIProxyHandler : IHttpHandler, IRouteHandler
{
public const string SCHEME_DELIM = "://";
public const string PROXY_IDENTIFIER = "proxy";
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
bool isLocal = false;
string url = context.Request.Url.OriginalString.ToLower();
int eOfSchemeIndx = url.IndexOf(SCHEME_DELIM) + SCHEME_DELIM.Length;
int eOfProxyIndx = url.IndexOf(PROXY_IDENTIFIER) + PROXY_IDENTIFIER.Length + 1;
url = url.Remove(eOfSchemeIndx, eOfProxyIndx - eOfSchemeIndx);
if (url.Contains(SCHEME_DELIM + "localhost"))
{
url = url.Replace(SCHEME_DELIM + "localhost", SCHEME_DELIM + "localhost:" + context.Request.Url.Port);
isLocal = true;
}
var handler = new HttpClientHandler { UseDefaultCredentials = true };
var httpClient = new HttpClient(handler);
string method = (isLocal) ? "GET" : context.Request.HttpMethod;
httpClient.DefaultRequestHeaders.Add("ep-ad-user-name", generateApiToken(context.Request.LogonUserIdentity.Name));
HttpResponseMessage response = new HttpResponseMessage();
if (method != "GET")
{
using (Stream stream = context.Request.GetBufferedInputStream())
{
byte[] data = new byte[context.Request.ContentLength];
stream.Read(data, 0, context.Request.ContentLength);
StringContent content = new StringContent(ASCIIEncoding.ASCII.GetString(data), Encoding.UTF8, "application/json");
if (method == "POST")
response = httpClient.PostAsync(url, content).Result;
else if (method == "PUT")
response = httpClient.PutAsync(url, content).Result;
}
}
else
{
response = httpClient.GetAsync(url).Result;
}
// read data from response
string serverResponse = response.Content.ReadAsStringAsync().Result;
serverResponse = serverResponse.Replace("http://", "/proxy/");
HttpResponse httpResponse = context.Response;
httpResponse.ContentType = "application/json; charcode=utf8";
httpResponse.StatusCode = (int)response.StatusCode;
httpResponse.Write(serverResponse);
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment