0
私はすべてを試しましたが、WebApiプロジェクトでCORSを有効にすることはできません。私は何かが欠落しているか、そうしていないと思う。 私StartUp.configは次のとおりです。CORA for web apiを有効にできません
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
// Web API routes
//app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
config.MapHttpAttributeRoutes();
config.EnableCors(new System.Web.Http.Cors.EnableCorsAttribute("http://www.test.ca", "*", "GET,POST")); //enable only for this domain
ConfigureOAuth(app);
app.UseWebApi(config);
ConfigureAutofac(app, config);
}
マイAPIコントローラ:
[HttpPost]
[Authorize]
[Route("api/Accounts/GetTestTest")]
[System.Web.Http.Cors.EnableCors("http://www.test.ca", "*", "*")]
public HttpResponseMessage GetTestTest()
{
return this.Request.CreateResponse(System.Net.HttpStatusCode.OK);
}
ここ
私の要求がローカルホスト上で動作するMVCアプリケーションから作られているので、私は制限する必要があります。また、ユーザーを承認するためにトークンを使用しています。 私が逃していることや間違っていることは何ですか?
EDIT要求は次のようにMVCコントローラのアクションから来ている:
static string CallApi(string url, string token, LogInRequest request)
{
System.Net.ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
using (var client = new HttpClient())
{
if (!string.IsNullOrWhiteSpace(token))
{
var t = Newtonsoft.Json.JsonConvert.DeserializeObject<CashManager.Models.Global.Token>(token);
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + t.access_token);
}
var response = client.PostAsJsonAsync<string>(url,string.Empty).Result;
return response.Content.ReadAsStringAsync().Result;
}
}
フロントエンドのMVCまたはJavaScriptのC#バックエンドからのリクエストですか? – juunas
編集をご覧ください。 CallApiメソッドを使用しているMVCコントローラからのリクエストです – Gamaboy