Hide link based on Role

前端 未结 1 1647
难免孤独
难免孤独 2021-01-11 17:06

Im new to asp.mvc. I\'m trying to develop a portal to maintain employee data. In my system only \"Manager\" has the rigths to create employee. How do I enable the link when

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-11 17:36

    You could use roles. The first and most important thing is to decorate the controller action that is supposed to perform the update with the Authorize attribute and specify the correct roles that the user must posses in order to access this controller action:

    [HttpPost]
    [Authorize(Roles = "Managers")]
    public ActionResult Create(Employee emp)
    {
        ...
    }
    

    Once everything is secure on the server you could do cosmetics in the view and show the link only if the user is in the Managers role:

    @if (User.IsInRole("Managers"))
    {
        @Html.ActionLink("Create employee", "Create")
    }
    

    You may take a look at the following article for more information about forms authentication and roles.

    0 讨论(0)
提交回复
热议问题