Reputation: 6850
Trying to figure out the best way to get the web page the user is coming from in MVC3.
I am building a download file function and if the user hasn't been linked to the file from my own website I want to redirect them to a page of my choosing.
I basically want to stop people from directly linking to my files without passing through my web site first.
How would i do this in the most reliable way?
Upvotes: 2
Views: 265
Reputation: 6850
This is how I've solved it for now. If a request for a file comes in the page to show info about the file is displayed. To download the file the user clicks a link pointing to the same page.
When the code detects that the user is requesting a file from the files own URL the file is served to the user.
private bool RequestIsCommingFromSamePage()
{
if (Request.UrlReferrer == null)
return false;
string requestURL = Request.UrlReferrer.ToString();
string pageURL = Request.ServerVariables["HTTP_HOST"] + Request.RawUrl;
requestURL = requestURL.Replace("http://", "").Replace("https://", "");
if (requestURL == pageURL || pageURL == null || requestURL == null)
return true;
return false;
}
Upvotes: 0
Reputation: 3835
The super simple way (that would work most of the time), would be to take a look at Request.UrlReferrer
in your controller. That should contain the URL the visitor came from.
Upvotes: 3
Reputation: 3177
MVC3 still has ASP.NET as base. So you can use all of the features of ASP.NET too...
Another way is to use an IIS-Extension => https://www.iis.net/community/default.aspx?tabid=34&g=6&i=1288 (LeechGuard from Microsoft)
Upvotes: 1