ASP.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

ASP Source Code


Welcome to Dream.In.Code
Become a ASP.NET Expert!

Join 300,456 ASP.NET Programmers for FREE! Get instant access to thousands of ASP.NET experts, tutorials, code snippets, and more! There are 1,635 people online right now. Registration is fast and FREE... Join Now!





Check for valid HttpRequest

This is a snippet I use for making sure a page request is coming from my domain, and that someone isn't trying to hijack my site or page.

Submitted By: PsychoCoder
Actions:
Rating:
Views: 1,471

Language: ASP

Last Modified: February 27, 2008
Instructions: This snippet is for use in a class file (not a code behind class file). Pass the method your HttpRequest Object and the domain you're looking for, it will then search that HttpRequest Object's ServerVariables for a valid HTTP_REFERER. Check the "Sample Usage" portion for how to implement

Snippet


  1. //Namespace Reference
  2. using System.Web;
  3. using System.Collections.Specialized;
  4.  
  5.  
  6. #region IsValidRequest
  7. /// <summary>
  8. /// method for determining if this page was called from a valid
  9. /// domain or if someone is trying to hijack this page
  10. /// </summary>
  11. /// <returns>True/False</returns>
  12. public bool IsValidRequest(System.Web.HttpRequest request,string domain)
  13. {
  14.  
  15.     //NameValueCollection object for holding the server variables
  16.     NameValueCollection vars = request.ServerVariables;
  17.     string temp = vars.Get("HTTP_REFERER");
  18.     //make sure the referer isnt empty, if it's
  19.     //empty this isn't a valid request
  20.     if (!(string.IsNullOrEmpty(temp)))
  21.     {
  22.         //ok, so its not empty, now lets make sure the user
  23.         //is coming from a valid page
  24.         if (!(temp.Contains(domain)))
  25.         {
  26.             //not a valid request so return false
  27.             return false;
  28.         }
  29.         else
  30.         {
  31.             //request originated on a valid page so return true
  32.             return true;
  33.         }
  34.     }
  35.     else
  36.     {
  37.         //HTTP_REFERER is empty so this isn't a valid request, return false
  38.         return false;
  39.     }
  40.  
  41. }
  42. #endregion
  43.  
  44. //Sample Usage
  45.  
  46. //replace YourClass with the name of the class where this resides
  47. YourClass check = new YourClass();
  48.  
  49. //now check the validity
  50. if(!(check.IsValidRequest(Request,"yourdomain.com")))
  51. {
  52.     Response.Redirect("http://www.yoursite.com");
  53. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live ASP.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

ASP.NET Tutorials

Reference Sheets

ASP.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month