开发者

Catching ASP.Net MVC Principle Permission attribute exceptions

开发者 https://www.devze.com 2022-12-15 19:46 出处:网络
I would like to secure my MVC controller actions using... [PrincipalPermission(SecurityAction.Demand, Role=\"Administrator\")]

I would like to secure my MVC controller actions using...

[PrincipalPermission(SecurityAction.Demand, Role="Administrator")]

However, if the user is not in this role then a SecurityException "Request for principal permission failed." is thrown by the code.

There seems to be no way to handle this error, even [Handle] error wont catch it.

What I would like is a way to catch the security exceptions and then redirect the user to my Login page (or route).

Or, some way to write my o开发者_如何学运维wn action filter that I can add to any method e.g.

[MustBeAnAdministrator]

... which would check if the user is in the correct role, and redirect them. However, I cant seem to get a redirection to work in an action filter.

Thanks.


In our MVC-project we use the

<Authorize(Roles:="Administrators")>

Function given by the MembershipProvider - which we fitted to our conditions. If the user is not logged in he will be forwarded to the login-screen.

0

精彩评论

暂无评论...
验证码 换一张
取 消