I have an angular web application deployed to server1 and a web api exists in server2. Our architects are saying that there will be firewalls enabled in server2 so that all the request will be blocked except from server1 [website]. That means, I will not be able to directly submit requests from my angular services [js files]. So they want me to create a WebApi Controller in my angular website which would do a pass through submission to the web api and fetches the result back. Has any one done this before? If so, could you provide some guidance?
Asked
Active
Viewed 676 times
0
-
1Wouldn't be easier to elaborate some kind of authentication in the API of server2? Some kind of trust key that only you know and can be validated on the server side? And how does the server2 knows you are sending the request from server1? Doesn't make much sense.. – jpgrassi Dec 03 '15 at 22:46
-
@jpgrassi, the webapi is already deployed to server2 and changes to it is limited. I am not a network expert. But what I understood is, there will be very limited servers in the DMZ where server2 and sever1 are existing and it will be completely blocked from all the requests coming outside of that DMZ. I do not know how they do it. But I verified that I could not test it locally from my machine. When I deployed my changes to server1, I was able to make it to work. – user007 Dec 04 '15 at 16:00
1 Answers
0
I got some help from here Below is my class:
using System.Configuration;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading.Tasks;
using System.Web.Http;
namespace MyWebsite.Controllers
{
public class ProxyController : ApiController
{
private readonly string _baseUrl = ConfigurationManager.AppSettings["WebApiBaseUrl"];
/// <summary>
/// Generic Pass through Get Method
/// </summary>
/// <param name="url">Url for Get Submission</param>
/// <returns></returns>
[HttpGet]
public async Task<object> GetAsync(string url)
{
var client = new HttpClient();
var response = await client.GetAsync(_baseUrl + url);
return await response.Content.ReadAsAsync<object>();
}
/// <summary>
/// Generic Pass through Post Method
/// </summary>
/// <param name="url">Url for Post Submission</param>
/// <param name="data">Data for Post</param>
/// <returns></returns>
[HttpPost]
public async Task<object> PostAsync(string url, object data)
{
var client = new HttpClient();
var response = await client.PostAsync(_baseUrl + url, data, new JsonMediaTypeFormatter());
return await response.Content.ReadAsAsync<object>();
}
/// <summary>
/// Generic Pass through Put Method
/// </summary>
/// <param name="url">Url for Put Submission</param>
/// <param name="data">Data for Put</param>
/// <returns></returns>
[HttpPut]
public async Task<object> PutAsync(string url, object data)
{
var client = new HttpClient();
var response = await client.PutAsync(_baseUrl + url, data, new JsonMediaTypeFormatter());
return await response.Content.ReadAsAsync<object>();
}
/// <summary>
/// Generic Pass through Delete Method
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
[HttpDelete]
public async Task<object> DeleteAsync(string url)
{
var client = new HttpClient();
var response = await client.DeleteAsync(_baseUrl + url);
return await response.Content.ReadAsAsync<object>();
}
}
}
I have not tested the PUT and DELETE. But already tested GET and POST. It is working fine.

user007
- 1,504
- 2
- 18
- 51