How to show authorization error message in ASP.NET MVC 2 application?

I use the Authorize attribute to filter controller actions based on user roles, but if an unauthorized action is attempted, the user is redirected back to the login page. As a user, I would find this confusing and annoying.

How can I instead display an error message informing the user about the need for certain roles and stay in the view where they tried to perform the action?

+3
source share
2 answers

You will need to write your own attribute Authorizethat does not return HttpUnauthorizedResult. Also, staying on the same view will be a difficult task, as you may need to keep the whole context after the request.

+2
source

The solution I found so far is that you need to save the session for the current page in _ViewStart (maybe something else in MVC2), and then use this in your authorization class to redirect to the page and display the message.

0
source

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


All Articles