仅为HttpModule中的特定域启用CORS

问题描述 投票:0回答:1

我有asp.net Web表单应用程序,我想只为特定域启用CORS。我试图使用HttpModule实现这一目标。

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

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

asp.net cors httpmodule
1个回答
0
投票

这可能是你正在寻找的。将using System.Web;和下面的代码放入Application_BeginRequest事件的Global.asax文件中。

        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();
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.