Passing data from AuthorizeCore to HandleUnauthorizedRequest, then action in ASP.NET MVC4

I have a custom implementation of AuthorizeAttribute that handles unauthorized user access, I redefined the following methods: AuthorizeCore , then HandleUnauthorizedRequest .

  • User access control logic is located in AuthorizeCore.
  • Redirecting to the controller / action is in the HandleUnauthorizedRequest .

Now I would like to know how can I send data from AuthorizeCore to the controller / action? Following this sequence:

  • AuthorizeCore create a message.
  • HandleUnauthorizedRequest redirect and pass the previously created message.
  • The controller / action receives the message.

Note. I do not want to receive a message through a QueryString.

+4
source share
2 answers

There are several ways to do this. You can try adding:

 filterContext.Controller.TempData["auth_fail_message"] = "Error message"; 

in the HandleUnauthorizedRequest method. You can then access the temporary data from the error action.
You can also use the asp.net session object.

0
source

Try it;

 public string Messages { get; set; } // declare "Messages" protected override bool AuthorizeCore(HttpContextBase httpContext) { Messages = "bla bla";// Or set in controller .... .... .... .... base.AuthorizeCore(httpContext); } protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { Messages.ToString(); // Read Message ="bla bla" base.HandleUnauthorizedRequest(filterContext); } 

2.Method Set the message using the controller;

 [CustomAuthorize(Messages = "Bla Bla Bla")] public ActionResult Index() { return View(); } 
0
source

All Articles