Using state descriptions on HttpUnauthorizedResult

In my MVC application, I call the HttpUnauthorizedResult class and specify the statusDescription parameter.

if (!canAdd) { return new HttpUnauthorizedResult("You do not have access to add"); } 

This also redirects the Login method to AccountController and then redirects them to the appropriate screen.

 public ActionResult Login(string returnUrl) { if (WebSecurity.IsAuthenticated) { return RedirectToAction("AccessDenied"); } ViewBag.ReturnUrl = returnUrl; return View(); } 

My question is how can I use the Status Descripton parameter, it would be nice to display this data in the AccessDenied view.

+6
source share
1 answer

It turns out the same problem. I used TempData to install the message because it is not possible to install the Viewbag.

 filterContext.Controller.TempData["message"] = "Access Denied"; filterContext.Result = new HttpUnauthorizedResult(); 

The HttpUnauthorizedResult redirects me to the login action of my controller account, where I check for messages (errors):

 if (TempData.Count > 0) { var message = TempData["message"]; ModelState.AddModelError("", message.ToString()); } 
+1
source

Source: https://habr.com/ru/post/953119/


All Articles