How to show part of an aspx page based on login user roles

I have an asp.net website. Each page has some content on it, and each journal user has several roles. I want to hide and show page content based on user log roles. I'm doing it? Thank you PS: I know about the authentication and authorization form in asp.net ... my problem is with the content of the page.

or

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

+4
source share
2 answers

I finally found my corresponding answer. I don’t want to set the visibility property (and yes it’s easy to guess!) Because it makes my code dirty and it’s not convenient ... Suppose I have a Default page. aspx and there are 3 roles Admin, A, B .... on the Default.aspx page. I want to show content based on user roles, so I use Loginview and its 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

are userControls! and here is my server server.

 protected void Page_Load(object sender, EventArgs e) { } 

I think this is better than setting visibility, because sometimes it’s hard to cope with it !! when a user with a role registered in the only content that is displayed is RoleA: RoleAPart part!

+7
source

It should not be too difficult: wrap the part (s) of the site that you want to show / hide based on the roles in the panel (or any other element that can fit you to complete the task). In your code, behind the_Load page (or Page_Init not sure about this) do something like

 if (Roles.IsUserInRole("rolename") { content.Visible = true; } else { content.Visible = false; } 

Where content is the panel id on your aspx page. The above code can also be simply written as

 content.Visible = Roles.IsUserInRole("rolename"); 

Another way to check roles would be

 User.IsInRole("rolename") 

which is actually a method that I prefer to use because it looks a little cleaner. The Roles.IsUserInRole method can also be used to check whether a particular user has a specific role, but we are not interested in this situation.

+5
source

Source: https://habr.com/ru/post/1489215/


All Articles