In my ASP.NET MVC app, I have most controllers decorated with
[Authorize(Roles="SomeGroup")]
When a user is not authorized to access something, they are sent to “~/Login” which is the Login action on my Account controller.
How can I determine that a user has reached the login page because of not being authorized so that I can show an appropriate error?
You can look for the
?ReturnUrl=querystring value, or you can create your own authorization filter & set a field inTempDataindicating the reason.Here is a simple custom filter that will do the trick:
Then in your view, you can do something like this:
(Though I’d recommend a better approach than these magic strings, but you get the point)