Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpContext.Current.Request.ServerVariables["HTTP_REFERER"] null

Tags:

c#

asp.net

I am trying to use following code to get referring url in global.asax session_start:

HttpContext.Current.Request.ServerVariables["HTTP_REFERER"]

I tried using Request.UrlReferrer.AbsoluteUri but UrlReferrer is also null.

but I am getting null. Can you please suggest me what is wrong or alternative?

like image 943
DotnetSparrow Avatar asked Feb 20 '23 14:02

DotnetSparrow


2 Answers

Not all user-agents send a referrer, some proxies/intermediaries strip the referrer, and often there simply is no referrer.

Just check whether Request.UrlReferrer == null at some point; if it is, don't try looking at Request.UrlReferrer.AbsoluteUri.

There is nothing "wrong" here, and nothing you can do about it. If you don't know where they came from, you'll just have to live with that.

like image 152
Marc Gravell Avatar answered Mar 04 '23 21:03

Marc Gravell


I know this answer is about 2 1/2 years late but I couldn't find a thorough write up on the UrlReferrer property so I figured I would add this info here. @MarcGravell's answer is correct for the most part, but it misses one other possibility. The value for referrer specified in the HTTP header can also be an invalid uri.

For this reason, you should be careful when using the UrlReferrer property on HttpRequest. If you look at the code that is executed when UrlReferrer is called using something like ILSpy, you'll see that it tries to parse the value from the request header. If the value in that header is not a valid uri, you will get a System.UriFormatException.

What this means is that simply checking UrlReferrer for null before trying to access AbsoluteUri could leave you with unhandled exceptions if the referrer is not a valid uri. If you want either a valid Uri or null, you'll have to use Request.ServerVariables["HTTP_REFERER"] and then Uri.TryParse, or you'll have to wrap the Request.UriReferrer == null check in a try-catch.

I put together a quick demo to show the behavior of the UrlReferrer property. Take the following page as an example:

<%@ Page Language="C#" AutoEventWireup="true" %>
<html><body>
        <table border="1">
            <tr><td>Referrer</td><td><%= GetUrlReferrer() %></td></tr>
        </table>
</body></html>
<script runat="server">
public string GetUrlReferrer()
{
    try
    {
        return Request.UrlReferrer == null ? "(None)" : Request.UrlReferrer.ToString();
    }
    catch (Exception ex)
    {
        return Request.ServerVariables["HTTP_REFERER"] + " (from Server Variable)";
    }
}    
</script>

Set up this page to run under http://localhost/urlreferrertest.aspx, and then try calling it from Powershell with an invalid Uri for the referrer:

> $client = new-object System.Net.WebClient
> $client.Headers.Add("Referer", "http://www%2etest%2e.com/test.html")
> $client.DownloadString("http://localhost/urlreferrertest.aspx")

If you step through the code you'll see that the call to Request.UrlReferrer throws an exception, and that http://www%2etest%2e.com/test.html is returned by accessing the ServerVariable.

like image 25
rsbarro Avatar answered Mar 04 '23 20:03

rsbarro