Getting the HTTP Referrer in ASP.NET
You could use the UrlReferrer property of the current request:
Request.UrlReferrer
This will read the Referer HTTP header from the request which may or may not be supplied by the client (user agent).
Getting referrer in asp net mvc
Usually, Referrer URLs
are passed between two unrelated sites (from one site to another) when navigation occurs by clicking a link or JavaScript-based navigation. Referrer URLs
are not sent if the user uses the browsers address bar, back/forward buttons/ etc.. to navigate.
There are several reasons why the Referrer URL
is empty in a request.
For some (security/privacy) reasons, the
Referrer URL
is stripped out
when navigating from a HTTPS site to a HTTP site (e.g. from
https://google.com to http://example.com).It can also be stripped out using some other JavaScript
and HTML tricks.
once Referrer URL
has been stripped out, There is no way to disable this behavior to get it back.
How can I get Url Referrer in ASP.NET Core MVC?
You're almost there. The StringValues
class is just a type ASP.NET uses to efficiently represent strings in the framework. Especially in the HttpContext
object. You can just call ToString()
on it to convert it to a string:
string referer = Request.Headers["Referer"].ToString();
get referrer from a specific page in same domain C#
I was able to figure it out. Took some parts of Albert's code and made some changes to mine.
Uri referrer = HttpContext.Current.Request.UrlReferrer;
string urlName = Request.UrlReferrer.ToString(); // grabbing referring page address
if (referrer == null && urlName != "default.aspx")
{
Response.Redirect(url: "default.aspx", endResponse: false);
return;
}
if (!IsPostBack)
{
if(Session["customerID"] == null && urlName != "default.aspx") //If both are false they go to homepage
{
Response.Redirect(url: "default.aspx", endResponse: false);
}
else
{
customerInfo(); //or else they get the customer info on the customer page
}
}
How do I get the referrer URL in an ASP.NET MVC action?
You can use Request.UrlReferrer
to get the referring URL as well if you don't like accessing the Request.ServerVariables
dictionary directly.
Spoofing HTTP Referrer data using ASP.NET
I don't know if this exactly what you want, but in general, you should be able to spoof the value of the UrlReferer
property (even if it's read-only) in HttpContext.Current.Request
by using a bit of reflection.
For example:
FieldInfo fi = HttpContext.Current.Request.GetType().GetField("_referrer", BindingFlags.NonPublic | BindingFlags.Instance);
string initialReferer = HttpContext.Current.Request.UrlReferrer.ToString();
if (fi != null)
fi.SetValue(HttpContext.Current.Request, new Uri("http://example.com"));
string fakedReferer = HttpContext.Current.Request.UrlReferrer.ToString();
On VS; these are the values before and after changing the UrlReferrer:
initialReferer
"http://localhost/Test/Default.aspx"
fakedReferer
"http://example.com/"
If you open the System.Web assembly using ILSpy you'll notice that the UrlReferrer property looks something like this:
public Uri UrlReferrer
{
get
{
if (this._referrer == null && this._wr != null)
{
string knownRequestHeader = this._wr.GetKnownRequestHeader(36);
if (!string.IsNullOrEmpty(knownRequestHeader))
{
try
{
if (knownRequestHeader.IndexOf("://", StringComparison.Ordinal) >= 0)
{
this._referrer = new Uri(knownRequestHeader);
}
else
{
this._referrer = new Uri(this.Url, knownRequestHeader);
}
}
catch (HttpException)
{
this._referrer = null;
}
}
}
return this._referrer;
}
}
How to extract scheme, host, path, and query string from the HTTP Referer using C# ASP.NET Core
if you save the referrer as a string say refURL = Context.Request.Headers["Referer"].ToString()
Then
var address = new System.Uri(refURL);
var scheme = address.Scheme ;
var host = address.Host;
etc
details on Uri Class
How do I identify the referrer page in ASP.NET?
Look at the Segments property of the URI class (which is what HttpContext.Current.Request.UrlReferrer returns).
Something like HttpContext.Current.Request.UrlReferrer.Segments[1]
(changing the 1 indexer to get the correct segment you require).
Related Topics
Have a Set of Tasks with Only X Running at a Time
Combining N Datatables into a Single Datatable
Static VS Non-Static Class Members
How to Overload the Square-Bracket Operator in C#
Is Int[] a Reference Type or a Value Type
Changing the Value of an Element in a List of Structs
What Is the Memory Overhead of a .Net Object
How to Ignore Unknown Enum Values During JSON Deserialization
Observablecollection That Also Monitors Changes on the Elements in Collection
Deserializing JSON When Sometimes Array and Sometimes Object
How to Get Difference Between Two Dates in Year/Month/Week/Day
Why Does .Net Use Int Instead of Uint in Certain Classes
JSON.Net Disable the Deserialization on Datetime
How to Update UI from Another Thread Running in Another Class
Is There a Generic Constructor with Parameter Constraint in C#