ASP.NET MVC Attribute to only let user edit his/her own content

前端 未结 4 547
深忆病人
深忆病人 2020-12-04 20:29

I have a controller method called Edit in which the user can edit data they had created like so ...

public ActionResult Edit(int id)
{
    Submi         


        
4条回答
  •  不思量自难忘°
    2020-12-04 20:41

    @if (Request.IsAuthenticated && User.IsInRole("Student"))
        {
        @Html.ActionLink("Edit", "Edit", new { id = item.StdID })
        }
    

    in my case, the loggedIn user is a student. so i say if the login request is authenticated, and if his role is student, then let the link for edit be accessible to him.

    this below allows you to let the ordinary user OR the Admin perform edit also.

    @if(Request.IsAuthenticated && User.IsInRole("Student") || 
    User.IsInRole("Administrator"))
    {
     @Html.ActionLink("Edit", "Edit", new { id = item.StdID })
    }
    

提交回复
热议问题