开发者

ASP.NET MVC 2 A problem with OnActionExecuting method

I have a controller called "SomeController". I want to check if the user is logged in or if has persissions to execute any action in that controller. To do so, I read that article http://blog.wekeroad.com/blog/aspnet-mvc-securing-your-controller-actions/ and I've written my own class (a test):

public class BaseFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            FormsAuthentication.RedirectToLoginPage();
        }
        //here will be checking the user permissions if he's logged in
    }
}

[BaseFilter]
public class SomeController : BaseController
{
 ...
}

but as You can understand it makes an infinitive loop when I want to ru开发者_开发技巧n any action from that controller. So, how to cope with that ?


You can apply the action filter on the relevant methods instead of at the class level.

Personally I would name this something like Authorize and then apply it to the controller methods that require authorization.

[Authorize]
public ActionResult Index()
{
// Do stuff
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜