Share Pastebin
Guest
Public paste!

Custom Filter

By: a guest | Jun 8th, 2010 | Syntax: C# | Size: 0.72 KB | Hits: 265 | Expires: Never
Copy text to clipboard
  1.  public class OnlyAuthenticatedFilterAttribute : ActionFilterAttribute  
  2.     {
  3.         public OnlyAuthenticatedFilterAttribute()
  4.         {
  5.            
  6.         }
  7.  
  8.         public override void OnActionExecuting(ActionExecutingContext ActionContext)
  9.         {
  10.                  if (!ActionContext.HttpContext.User.Identity.IsAuthenticated)
  11.                  {
  12.                      string returnUrl = "~/Login/?ReturnUrl=" + HttpContext.Current.Request.RawUrl;                  
  13.                      ActionContext.HttpContext.Response.Redirect(returnUrl, true);
  14.                      
  15.                  }
  16.  
  17.         }
  18. }
  19.  
  20.  
  21. [OnlyAuthenticatedFilterAttribute()]
  22.    public ActionResult Index()
  23.    {
  24.          return View();
  25.     }