Forum Moderators: open

Message Too Old, No Replies

Show Content Only If Logged In.any thoughts?

         

IntegrityWebDev

6:04 pm on Sep 3, 2010 (gmt 0)

10+ Year Member



I am drawing a blank on how to do this in C# ASP.NET and thought I'd pick your brain.

On most pages of my site I want to show certain content only if they are logged in, but show a generic "you must be logged in to view this content" message if they are not logged in.

I have a login test controlled by a session variable set to true or false.

What I'd like to do on every page that needs this is to put all the content in a panel, say PanelIsLoggedIn. Then have some kind of global function to check to see if the session var is true or false. True, show the content (show the panel). False, show the generic message (hide the content panel and show the generic panel).

Any thoughts on how to do this?

Thanks,
Chris

marcel

5:10 am on Sep 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would use the asp:LoginView control for this:

<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate></AnonymousTemplate>
<LoggedInTemplate></LoggedInTemplate>
</asp:LoginView>


After rereading your post I see that you are storing the login info in a session variable, you're not using ASP.NET Membership?

IntegrityWebDev

8:04 pm on Sep 5, 2010 (gmt 0)

10+ Year Member



I thought of that but because of the situation I can't use a standard log in. The login is an account number only that is stored in a DB, no password. I know, not standard or exactly safe but its not a security measure per se but for something else (they can only see info if they have an existing account number which is entirely on another system and imported into the db each night). I'm storing a boolean in a session to see if they are logged in or not.

marcel

8:17 am on Sep 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I usually do something like this, as I prefer session variables to be strongly typed and accessible from anywhere.

Define a property in either a BasePage or in a MasterPage (or in a seperate class):
Private _isLoggedIn As Boolean

Public Property IsLoggedIn() As Boolean
Get
_isLoggedIn = CType(Session("IsLoggedIn"), Boolean)
If _isLoggedIn = Nothing Then
_isLoggedIn = False
End If
Return _isLoggedIn
End Get
Set(ByVal value As Boolean)
_isLoggedIn = value
Session("IsLoggedIn") = _isLoggedIn
End Set
End Property


And then show or hide PlaceHolders depending on the value of IsLoggedIn:
<asp:PlaceHolder ID="plcAnonymous" runat="server">Anonymous</asp:PlaceHolder>
<asp:PlaceHolder ID="plcLoggedIn" runat="server">Logged in</asp:PlaceHolder>

plcLoggedIn.Visible = IsLoggedIn
plcAnonymous.Visible = Not IsLoggedIn


* I haven't tested this code, so it may contain some errors, but you should get the general idea.

IntegrityWebDev

12:30 pm on Sep 7, 2010 (gmt 0)

10+ Year Member



Great idea, I will port that over to C# and play with that. Thanks!

marcel

1:13 pm on Sep 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am drawing a blank on how to do this in C# ASP.NET...

I will port that over to C# and play with that

Sorry, I was confused. For some reason I was sure you said VB...