Solved

Controls based on roles or logged in

Posted on 2007-11-29
8
560 Views
Last Modified: 2012-05-05
How do I wrap code around a user control in order to indicate that it should only be displayed if the user has a certain role?

Same also for only displaying if a user is logged in?

I know how to register one and put it on the page, I just don't know how to do the if/else stuff.

thanks!


0
Comment
Question by:BobCSD
  • 5
  • 3
8 Comments
 
LVL 22

Expert Comment

by:CJ_S
Comment Utility
Two options imo.
1) use the provided control (asp:LoginView) and use the appropriate template
2) use a little bit of code (<asp:.... visible='<%# Page.User.IsAuthenticated%>' /> or <asp:.... visible='<%# Page.User.IsInRole("administrator")%>' />)


0
 
LVL 1

Author Comment

by:BobCSD
Comment Utility
when I use that on a control:
Visible="<%# Page.User.IsInRole("administrator")%>"

I get an error: Parser Error Message: The server tag is not well formed.

<uc:RBox ID="RBox1" runat="server" Visible="<%# Page.User.IsInRole("administrator")%>" />

Isn't there a way to do multiple roles?

Or if the user is NOT in the role?

0
 
LVL 22

Accepted Solution

by:
CJ_S earned 500 total points
Comment Utility
Well - you didn't copy the code as I gave you. Note the different quotation marks

Visible='<%# Page.User.IsInRole("administrator")%>'

Multiple roles:
Visible='<%# (Page.User.IsInRole("administrator") or Page.User.IsInRole("editor"))%>'

Not in a role (using C#)
Visible='<%# !Page.User.IsInRole("administrator")%>'

Not in a role (using VB.Net)
Visible='<%# Not Page.User.IsInRole("administrator")%>'


0
 
LVL 1

Author Closing Comment

by:BobCSD
Comment Utility
great answer!
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:BobCSD
Comment Utility
0
 
LVL 1

Author Comment

by:BobCSD
Comment Utility
Ugh. I graded too soon.

this:
<uc:BannerAdvfn ID="Banner1" runat="server" visible='<%# Page.User.IsAuthenticated%>'  />

gives this error:
Compiler Error Message: BC30456: 'IsAuthenticated' is not a member of  'System.Security.Principal.IPrincipal'.
0
 
LVL 22

Expert Comment

by:CJ_S
Comment Utility
My bad

'<%# Page.User.Identity.IsAuthenticated%>'
0
 
LVL 1

Author Comment

by:BobCSD
Comment Utility
Well, even that doesn't seem to work with single or double quotes (it says I have to have double to match the rest of my control), or NOT or not NOT. It always displays whether logged in or not:

<uc:MenuAd ID="Menu2" runat="server" Visible="<%# Page.User.Identity.IsAuthenticated%>"/>

But that's okay. Someone else showed me how to define it in the page load... which works better for my purposes anyway.

thanks.


        If isLoggedIn Then ' defined above.....
            Me.Menu1.Visible = False
        Else
            Me.Menu2.Visible = False
        End If
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now