如何根据登录用户角色显示aspx页面的某些部分 [英] how to show some part of aspx page based on a login user roles

查看:80
本文介绍了如何根据登录用户角色显示aspx页面的某些部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net网站..每个页面上都有一些内容,每个登录用户都有一些角色..我想根据登录用户角色来隐藏和显示页面内容.我做那个吗?谢谢.附言:我知道asp.net中的身份验证和授权"表格...我的问题是有关页面内容的.

I have an asp.net site..each page have some contents on it and each log in user have some roles..I want to hide and show contents of the page based on the log in user roles..how can I do that? Thanks. P.S : I Know about Authentication and Authorization form in asp.net...my problem is about contents of a page.

我想知道是否有一些.ascx(用户控件),然后将它们添加到.aspx页面中是什么.然后基于可见性属性和用户角色隐藏并显示了aspx页面的一部分.最好的办法?谢谢.

I want to know is it right to have some .ascx (user controls) and then add them to the .aspx page..then based on visibility property and user roles hide and show parts of the aspx page..is this the best way? Thanks.

推荐答案

我终于找到了合适的答案.我不想设置可见性属性(是的,不难猜测!),因为它使我的代码成为可能脏并且不方便...假设我有Default.aspx页,并且在Default.aspx页中有3个角色Admin,A,B....我想根据用户角色显示内容,所以我使用Loginview和它是模板,如下所示:

I finally found my appropriate answer..I do not want to set visibility property (and yes it is not hard to guess!) ,because it makes my code dirty and it's not convenient...suppose I have page Default.aspx and there are 3 roles Admin,A,B....in the Default.aspx page I want to show contents based on user roles,so I use Loginview and it's Templates as shown below:

<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>

RoleB:RoleBPart,RoleA:RoleAPart,Admin:AdminPart

RoleB:RoleBPart, RoleA:RoleAPart, Admin:AdminPart

是userControls!这是我的服务器端.

are userControls! and here is my serverside..

protected void Page_Load(object sender, EventArgs e)
    {

    }

我认为这比设置可见性更好,因为有时很难处理它!当具有角色A的用户登录时,唯一显示的内容是RoleA:RoleAPart零件!

I think it is better than setting visibility because sometimes it gets difficult to handle it!! when user with role A logged in the only content which is displayed is RoleA:RoleAPart part!

这篇关于如何根据登录用户角色显示aspx页面的某些部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆