我有一个 ASP.NET 站点,其中每个页面都有一些内容,每个登录用户都有一些特定的角色。我想根据登录用户的角色隐藏和显示页面内容。我怎样才能做到这一点?
P.S.:我了解 ASP.NET 中整个表单的身份验证和授权;我的问题是关于页面的内容。
我是否必须有一些 .ascx (用户控件),然后将它们添加到 .aspx 页面,然后根据可见性属性和用户角色隐藏和显示 aspx 页面的部分内容?或者是通过其他方式完成的?
我不想设置可见性属性,因为它使我的代码变脏并且不方便。假设我有一个 Default.aspx 页面,并且有 3 个角色:
在 Default.aspx 页面中,我想根据用户角色显示内容,因此我使用 Loginview 及其模板,如下所示:
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:LoginView runat="server" ID="loginviewControl1">
<AnonymousTemplate>
<asp:HyperLink runat="server" ID="lnkLogin" Text="Log In" NavigateUrl="~/Account/Login.aspx"></asp:HyperLink>
<Anonymous:AnonymousPart ID = "anonym" runat="server" />
</AnonymousTemplate>
<LoggedInTemplate>
<asp:Label runat="server" ID="WelcomeBackMessage"></asp:Label>
</LoggedInTemplate>
<RoleGroups>
<asp:RoleGroup Roles="Admin">
<ContentTemplate>
<Admin:AdminPart ID ="adminContent" runat="server" />
</ContentTemplate>
</asp:RoleGroup>
<asp:RoleGroup Roles="A">
<ContentTemplate>
<RoleA:RoleAPart ID = "RoleAContent" runat="server"/>
</ContentTemplate>
</asp:RoleGroup>
<asp:RoleGroup Roles="B">
<ContentTemplate>
<RoleB:RoleBPart ID = "RoleBContent" runat="server" />
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
</asp:Content>
RoleB:RoleBPart
, RoleA:RoleAPart
, and Admin:AdminPart
是用户控件。
这是我的CS文件代码:
protected void Page_Load(object sender, EventArgs e)
{
}
我认为这比设置可见性更好,因为有时很难处理它。当角色A的用户登录时,显示的内容只有RoleA:RoleAPart部分。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)