Access-Control-Allow-Origin Multiple Origin Domains?

前端 未结 30 2105
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 07:08

Is there a way to allow multiple cross-domains using the Access-Control-Allow-Origin header?

I\'m aware of the *, but it is too open. I rea

30条回答
  •  灰色年华
    2020-11-21 07:20

    To facilitate multiple domain access for an ASMX service, I created this function in the global.asax file:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        string CORSServices = "/account.asmx|/account2.asmx";
        if (CORSServices.IndexOf(HttpContext.Current.Request.Url.AbsolutePath) > -1)
        {
            string allowedDomains = "http://xxx.yyy.example|http://aaa.bbb.example";
    
            if(allowedDomains.IndexOf(HttpContext.Current.Request.Headers["Origin"]) > -1)
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", HttpContext.Current.Request.Headers["Origin"]);
    
            if(HttpContext.Current.Request.HttpMethod == "OPTIONS")
                HttpContext.Current.Response.End();
        }
    }
    

    This allows for CORS handling of OPTIONS verb also.

提交回复
热议问题