Hiding links from certain roles in ASP.NET MVC5

烈酒焚心 提交于 2019-12-03 05:40:45

Depending on what sort of Membership/User provider you are using, you should just be able to check directly from the View if the user is logged in and in the specific role.

So you would end up with something like;

@Html.ActionLink("Index", "Home") 
@Html.ActionLink("About", "Home") 
@Html.ActionLink("Contact", "Home") 
@if ( User.Identity.IsAuthenticated ){
    if ( User.IsInRole("Admin") ){
        @Html.ActionLink("Admin", "AdminController")        
    }
}

And remember to add [Authorize] attribute to your Admin action method:

[Authorize(Roles="Admin")]
public ActionResult Admin()
{
    // ...
    return View();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!