Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. public class SignInRequired : ActionFilterAttribute
  2. {
  3. public override void OnActionExecuting(ActionExecutingContext filterContext)
  4. {
  5. // User is verified, continue executing action
  6.  
  7. if (Acme.Web.CurrentUser != null)
  8. {
  9. return;
  10. }
  11.  
  12. // End response with 401 Unauthorized
  13.  
  14. var response = HttpContext.Current.Response;
  15. response.StatusCode = (int)HttpStatusCode.Unauthorized;
  16. response.End();
  17.  
  18. // Prevent the action from actually being executed
  19.  
  20. filterContext.Result = new EmptyResult();
  21. }
  22. }
  23.  
  24. public class SignInRequiredAttribute : AuthorizeAttribute
  25. {
  26. protected override bool AuthorizeCore(HttpContextBase httpContext)
  27. {
  28. return !Acme.Web.CurrentUser != null;
  29. }
  30. }
  31.  
  32. public override void OnActionExecuting(HttpActionContext actionContext)
  33.  
  34. {
  35. actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
  36. }
  37.  
  38. filterContext.Result = new RedirectResult("~/Error/Unauthorized");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement