Solved

Controls based on roles or logged in

Posted on 2007-11-29
8
562 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:Starr Duskk
  • 5
  • 3
8 Comments
 
LVL 22

Expert Comment

by:CJ_S
ID: 20375838
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 2

Author Comment

by:Starr Duskk
ID: 20376752
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
ID: 20381122
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 2

Author Closing Comment

by:Starr Duskk
ID: 31411718
great answer!
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20402830
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20403075
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
ID: 20409974
My bad

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

Author Comment

by:Starr Duskk
ID: 20413756
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Not showing JavaScript in the list 5 53
Gridview selected row 9 46
Why does my excel data start at B1 instead of A1. 9 83
jquery datatables, asp.net MVC 9 41
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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