【问题标题】:Enable CORS only for specific domain from HttpModule仅为 HttpModule 中的特定域启用 CORS
【发布时间】:2019-08-27 16:27:07
【问题描述】:

我有一个 asp.net 网络表单应用程序,我想只对特定域启用 CORS。我正在尝试使用 HttpModule 来实现这一点。

例如我的网站是https://mysiteex.com

应该从https://third-party1.comhttps://third-party2.com 启用CORS

【问题讨论】:

    标签: asp.net cors httpmodule


    【解决方案1】:

    这可能是您正在寻找的。将 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();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2018-10-04
      相关资源
      最近更新 更多