【发布时间】:2019-08-27 16:27:07
【问题描述】:
我有一个 asp.net 网络表单应用程序,我想只对特定域启用 CORS。我正在尝试使用 HttpModule 来实现这一点。
例如我的网站是https://mysiteex.com
应该从https://third-party1.com 和https://third-party2.com 启用CORS
【问题讨论】:
标签: asp.net cors httpmodule
我有一个 asp.net 网络表单应用程序,我想只对特定域启用 CORS。我正在尝试使用 HttpModule 来实现这一点。
例如我的网站是https://mysiteex.com
应该从https://third-party1.com 和https://third-party2.com 启用CORS
【问题讨论】:
标签: asp.net cors httpmodule
这可能是您正在寻找的。将 using System.Web; 和下面的代码放入您的 Global.asax 文件中,用于 Application_BeginRequest 事件。
protected void Application_BeginRequest(object sender, EventArgs e)
{
// Enable CORS for cross-site scripting.
var context = HttpContext.Current;
var response = context.Response;
// enable CORS. "*" = all domains, substitute your own as needed.
response.AddHeader("Access-Control-Allow-Origin", "*");
if (context.Request.HttpMethod == "OPTIONS")
{
response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
response.End();
}
}
【讨论】: