我想根据身份验证状态或角色显示/隐藏视图的某些部分。对于我的控制器动作,我扩展了 ActionFilterAttribute,因此我可以为某些动作赋予属性。
<RequiresRole(Role:="Admin")> _
Function Action() as ActionResult
Return View()
End Function
我可以在视图中使用类似的方式(归因)吗?(所以不是这样:如何根据用户的角色创建具有不同显示的视图?)
我想根据身份验证状态或角色显示/隐藏视图的某些部分。对于我的控制器动作,我扩展了 ActionFilterAttribute,因此我可以为某些动作赋予属性。
<RequiresRole(Role:="Admin")> _
Function Action() as ActionResult
Return View()
End Function
我可以在视图中使用类似的方式(归因)吗?(所以不是这样:如何根据用户的角色创建具有不同显示的视图?)
您可以像这样从视图访问用户的登录角色:
<% 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 DELETE,以防止页面爬虫意外删除数据以获取我的页面。在我的例子中,我使用 delete() 方法扩展 JQuery 来补充 HTTP 动词。
我新这个存在,但花了一段时间才找到。这是我正在使用的:
<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>
这允许您根据登录状态或凭据向不同用户显示不同的内容。