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から取得し、現在アクセスしているクライアントと比較します。「クロスドメインアクセス」に1つのドメインのみを許可したいので、これを行う必要があります。