在 Global.asax.cs 文件中,我有允许跨域访问的代码。
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
if( // The requesting URL == "http://theproperdomain.com"){
EnableCrossDmainAjaxCall();
}
}
private void EnableCrossDmainAjaxCall()
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
但是在我的其余项目允许跨域访问之前,它必须首先检查请求域是否等于正确的域。即:“http://theproperdomain.com”
我有这个代码:
string properdomain = HttpContext.Current.Request.Url.AbsoluteUri;
但我不确定它是否最好用。
请提出你更好的主意。
编辑
我的域服务器是http://theproperdomain.com
我的休息服务所在的地方。
我将从我的 webconfig 中获取此域名并与当前访问的客户端进行比较。我需要这样做,因为我只想允许一个域进行“跨域访问”。