I have an Asp.net Web API project which has several CRUD methods.
On top of these methods, i want to add an Authorization service that reads the Authorization
header and prevent users of accessing the resources (if they are not authorized).
// Method on internal IP Project
public class InternalController : ApiController
{
public void Create(CreateRequest request)
{
// implement the method
}
}
// Method on public IP Project
public class ExternalController : ApiController
{
public async Task Create(CreateRequest request)
{
// validate Authorization header and throw exception if not valid
using (HttpClient client = new HttpClient())
{
string parameters = string.Format("param1={0}¶m2={1}", request.Param1, request.Param2);
client.BaseAddress = new Uri("http://192.168.1.1/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("api/internal/create?" + parameters);
response.EnsureSuccessStatusCode();
}
}
}
Is there any way of "Redirecting" the request from the External API to the Internal API more easily?
Right now, i have to manually re-create all the parameters that i receive in ExternalAPI and send them in the InternalAPI, even if they are the same.
Can i make HttpClient
automatically send the HttpRequestMessage (Request)
object that i have in ExternalAPI method?
2 Answers 2
When speaking about ASP.NET Web API. HttpClient will not automatically redirect you. When you have become response from internal service you can pass it to external. Or you can redirect your action like here
To make it correct redirection for client from REST point of view use HTTP Redirect Headers and repsonse codes. For example HTTP response code 302. And then client should be able to react on such response code and get redirect address from Location header. But it's about redirect for client.
When speaking about call of some internal services from your API from architecture. You have following alternatives:
- Call your internal service as class method
- Make service to service call
- Setup message queue or bus and your API will communicate with it through service bus.
Call your internal service as class method Very easy. No impact and delays for service call. But you should reference assembly and it's not always possible. Or such way could be not possible due to requirements
Make service to service call Has disadvantages: your services are tightly coupled, you have delay and should wait for response from internal service. It's considered as bad practice. But could be a good temporarily solution as first step to service bus.
Setup message queue or bus and your API will communicate with it through service bus. Your services are decoupled and independent. You shouldn't wait for response. But it's technically harder to set up and make your architecture and infrastructure more complex/
As summary There is no best way from the box for your architecture and you should select from alternatives based on your requirements.
1 Comment
Here is the sample code to Post the data to web api:-
var handler = new HttpClientHandler {UseDefaultCredentials = true};
using (var client = new HttpClient(handler))
{
client.BaseAddress = new Uri("https://IE url.com");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var postDataObject = new
{
SCName = properties.Site.PortalName,
TotalSites = properties.Web.Webs.Count
};
var jsonPostData = new JavaScriptSerializer().Serialize(postDataObject);
HttpContent content = new StringContent(jsonPostData, System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync("/controllerclassname/InsertUpdateDataOperation", content).Result;
if (response.IsSuccessStatusCode)
{
//Check the response here
// var webApiResponse = response.Content.ReadAsStringAsync().Result;
}
Comments
Explore related questions
See similar questions with these tags.
InternalController
andExternalController
on the same web project?