ASP.NET MVC 역할 기반 또는 인증 기반보기 렌더링을 적용하는 방법은 무엇입니까?
-
03-07-2019 - |
문제
인증 상태 또는 역할을 기반으로 뷰의 특정 부분을 보여 주거나 숨기고 싶습니다. 컨트롤러 작업의 경우 ActionFilterattribute를 확장하여 특정 작업을 수행 할 수 있습니다.
<RequiresRole(Role:="Admin")> _
Function Action() as ActionResult
Return View()
End Function
a 비슷한 내가보기에서 사용할 수있는 방법 (속성)? (그래서 이것을 좋아하지 않습니다 : 사용자의 역할에 따라 다른 디스플레이를 갖는보기를 어떻게 만들 수 있습니까?)
해결책
다음과 같은보기에서 사용자의 로그인 역할에 액세스 할 수 있습니다.
<% if (Page.User.IsInRole("Admin")) { %>
<td>
<%= Html.DeleteButton("delete", model.ID) %>
</td>
<% } %>
그리고 아마도 다음과 같은 확장 방법을 사용할 수 있습니다.
public static string DeleteButton(this HtmlHelper html,
string linkText, int id)
{
return html.RouteLink(linkText,
new { ID = id, action = "Delete" },
new { onclick = "$.delete(this.href, deleteCompleted()); return false;" });
}
분명히, JavaScript를 사용하여 컨트롤러 작업에 HTTP 삭제를 수행하여 페이지 크롤러가 실수로 데이터가 페이지를 가져 오는 것을 방지하지 못하게합니다. 내 경우에는 http 동사를 보충하기 위해 delete () 메소드로 jQuery를 확장하고 있습니다.
다른 팁
나는 이것이 존재했지만 찾는 데 시간이 걸렸다. 내가 사용하고있는 것은 다음과 같습니다.
<asp:LoginView runat="server">
<AnonymousTemplate>
You are not logged in yet. Please log in.
</AnonymousTemplate>
<RoleGroups>
<asp:RoleGroup Roles="Admin">
<ContentTemplate>
You are an Admin.
</ContentTemplate>
</asp:RoleGroup>
<asp:RoleGroup Roles="Customers">
<ContentTemplate>
You are a customer.
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
<LoggedInTemplate>
Simple Log in check
</LoggedInTemplate>
</asp:LoginView>
이를 통해 로그인 상태 또는 자격 증명에 따라 다른 사용자에게 다른 콘텐츠를 표시 할 수 있습니다.
제휴하지 않습니다 StackOverflow