我有一個Asp.net Web API項目,它有幾個CRUD方法。Asp.net Web Api重定向請求
在這些方法之上,我想添加一個授權服務,該服務讀取Authorization
標頭並阻止用戶訪問資源(如果它們未被授權)。
// 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();
}
}
}
是否有任何方法可以更輕鬆地將外部API的請求「重定向」到內部API?
現在,我必須手動重新創建在ExternalAPI中收到的所有參數,並將它們發送到InternalAPI中,即使它們相同。
我可以讓HttpClient
自動發送我在ExternalAPI方法中使用的HttpRequestMessage (Request)
對象嗎?
是'InternalController'和'ExternalController'在同一個web項目? – haim770 2013-04-30 09:18:32
不,他們是不同的項目 – Catalin 2013-04-30 09:21:56