Skip to content

Commit

Permalink
CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
junalmeida committed Sep 7, 2017
1 parent fd9431f commit 04dad07
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions tfs-onpremise-ntlm/RedirectController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
Expand All @@ -15,10 +16,18 @@ public class RedirectController : ApiController
public async Task<HttpResponseMessage> ToAnyHost()
{
var url = Request.RequestUri.ToString();
url = url.Replace(Program.BaseUrl + "/api/", "");

var ixApi = url.IndexOf("/api/", StringComparison.InvariantCultureIgnoreCase);
url = url.Substring(ixApi + 5);
url = url.Replace("http/", "http://");
url = url.Replace("https/", "https://");

var origin = (string)null;
if (Request.Headers.Contains("Origin"))
{
origin = Request.Headers.GetValues("Origin").FirstOrDefault();
}

var handler = new HttpClientHandler();

var username = ConfigurationManager.AppSettings["username"];
Expand All @@ -44,15 +53,26 @@ public async Task<HttpResponseMessage> ToAnyHost()
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));

HttpResponseMessage result;

if (Request.Method == HttpMethod.Get)
return await ToAnyHostGet(client, url);
result = await ToAnyHostGet(client, url);
else if (Request.Method == HttpMethod.Options)
return await ToAnyHostOptions(client, url);
result = await ToAnyHostOptions(client, url);
//else if (Request.Method == HttpMethod.Post)
// return await ToTfsPost(client, url);
else
throw new NotSupportedException();
// List data response.
if (!string.IsNullOrWhiteSpace(origin))
{
if (result.Headers.Contains("Access-Control-Allow-Origin"))
result.Headers.Remove("Access-Control-Allow-Origin");
result.Headers.Add("Access-Control-Allow-Origin", origin);
}

result.Headers.Add("Access-Control-Allow-Credentials", "true");
return result;
}
}

Expand Down

0 comments on commit 04dad07

Please sign in to comment.