code_junkie

Why does AuthorizeAttribute redirect to the login page for authentication and authorization failures?

Nov 11th, 2011
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. [Authorize(Roles = "CanDeleteTags")]
  2. public void Delete(string tagName)
  3. {
  4. // ...
  5. }
  6.  
  7. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
  8. public class AuthorizeAttribute : System.Web.Mvc.AuthorizeAttribute
  9. {
  10. protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
  11. {
  12. if (filterContext.HttpContext.Request.IsAuthenticated)
  13. {
  14. filterContext.Result = new System.Web.Mvc.HttpStatusCodeResult(403);
  15. }
  16. else
  17. {
  18. base.HandleUnauthorizedRequest(filterContext);
  19. }
  20. }
  21. }
  22.  
  23. // User was redirected here because of authorization section
  24. if (User.Identity != null && User.Identity.IsAuthenticated)
  25. Response.Redirect("Unauthorized.aspx");
Add Comment
Please, Sign In to add comment