开发者

MVC3 - Reading Controller Authentication Roles

I am populating a MVC3 navigation menu and I want to read my controller defined authenticati开发者_如何学Con roles for each of my SiteMap nodes.....is there a means to do so without using Reflection ?


You can use MVC SiteMap Provider to generate your menu. It has built in security trimming based on AuthrizeAttribute. You can extend it easily.


You can actually do this in your view (Razor sintax):

@if(User.IsInRole("role_name") {
    @* Show or not show the item depending on the user's role *@
}

Just change de @ for <% %> if you're using the WebFormsViewEngine instead of the RazorViewEngine.

The same works in your controller, by the way:

public ActionResult MyAction()
{
    if(User.IsInRole("role_name") 
    { }
}


I was more interesting in enumerating the Controllers....rather than checking a user's roles within a View. I was hoping to avoid using the CodePlex Sitemap provider.....I am currently using the Telerik MVC Extensions for this menu security trimming at the present.

I was hoping that MVC3 may have this functionality by now.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜