'Referral domain doesn't work in ASP.NET Webforms application
I have an old website developed in ASP.NET Webforms, I need to make changes to the main page where I have to check the referral domain name..
If referral domain name is abc1.com, abc2.com or abc3.com, then I have to redirect to a different page.
I tried the following code but none works.
Code #1 example
Uri myUri = Request.UrlReferrer;
if (myUri != null)
{
Response.Write("myUri.Host" + myUri.Host);
//Response.Redirect("https://www.example.com/page1?referal=" + myUri.Host, true);
}
Above example is not working as it always return null
Code #2 example
Since above code is not full proof, I tried to read the header information
NameValueCollection headers = base.Request.Headers;
for (int i = 0; i < headers.Count; i++)
{
string key = headers.GetKey(i);
string value = headers.Get(i);
base.Response.Write(key + " = " + value + "<br/>");
}
Values returned by above code doesn't show any correct referral domain as it always shows the main domain of the website under host Host = www.example.com | x-sp-edge-host = www.example.com | x-fb-host = www.example.com and not the referral domain.
Besides this, I also tried combination of different code with no success.
Any pointer where I could be going wrong.
I used inspect tool to dig further and it always show the referral domain from 3 domain but code is not able to trap it
Screenshot for your reference:
**CODE EXAMPLE in Page_LoadEvent **
if (!IsPostBack)
{
Response.Write("*** !IsPostBack<br><br>");
string requestedDomain = HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
Response.Write("requestedDomain HTTP_HOST" + requestedDomain);
Response.Write("<br> HTTP_REFERER" + HttpContext.Current.Request.ServerVariables["HTTP_REFERER"]);
Response.Write("<br> ALL_HTTP" + HttpContext.Current.Request.ServerVariables["ALL_HTTP"]);
Uri myUri = Request.UrlReferrer;
if (myUri != null)
{
Response.Write(Request.UrlReferrer.ToString());
}
else
{
Response.Write("****Request.UrlReferrer.ToString()<br><br>");
Response.Write("NULL");
Response.Write("****Request.UrlReferrer.ToString()<br><br>");
}
NameValueCollection headers = base.Request.Headers;
for (int i = 0; i < headers.Count; i++)
{
string key = headers.GetKey(i);
string value = headers.Get(i);
base.Response.Write(key + " = " + value + "<br/>");
}
}
This code always show main domain in header information and not the referral domain. while when i check using inspect tool in browser it show referral domain as shown in screenshot above as first entry.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

