我想根据身份验证状态或角色显示/隐藏视图的某些部分。对于我的控制器操作,我已经扩展了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>

这允许您根据用户的登录状态或凭据向不同用户显示不同的内容。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top