?
Solved

Asp.net masterpage hold login value while redirecting from one page to another. Need help!

Posted on 2011-09-07
6
Medium Priority
?
304 Views
Last Modified: 2013-11-05
Hello,
I have masterpage + a few aspx pages on it. On masterpage I have create a login code, which is working fine. But, when redirecting from one page to another, login value lost it. In my case, below code, value is on Literal control.

I have read some posts with session. Ok, I know how to use session variable, but only if pages build as standalone, and are not under MasterPage,

Login code:
Imports System.Data.OleDb
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Login_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login.Click
        Dim connect As String = "Provider=Microsoft.Ace.OleDb.12.0;Data Source=C:\Documents and Settings\DEJANC\Desktop\WebSite1\test.accdb"
        Dim query As String
        query = "Select Count(*) From Tabela1 Where Username = ? And Password = ?"
        Dim result As Integer = 0
        Using conn As New OleDbConnection(connect)
            Using cmd As New OleDbCommand(query, conn)
                cmd.Parameters.AddWithValue("", UserName.Text)
                cmd.Parameters.AddWithValue("", Password.Text)
                conn.Open()
                Session("Username") = UserName.Text
                result = DirectCast(cmd.ExecuteScalar(), Integer)
            End Using
        End Using
        If result > 0 Then
            Literal1.Text = "OK"
        Else
            Literal1.Text = "NOT OK"
        End If
    End Sub
End Class

Open in new window


Thank you in advance for help to anyone.
0
Comment
Question by:dejandejanovic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 57

Accepted Solution

by:
HainKurt earned 1336 total points
ID: 36498322
sessions are kept in memory as long as user does not close browser (and does stay idle for more than a defined timeout value ~15 min). So set a session variable and use wherever/whenever you want...

session("username")="HainKurt"

...

if session("username")="" then
  server.transfer("Login.aspx")
else
  server.Transfer("home.aspx")
end if
0
 

Author Comment

by:dejandejanovic
ID: 36498565
Hm, in my case Login.aspx is MasterPage, because all login controls are create in MasterPage.Master >> Masterpage ContentPlaceHolder, also upper code which is executing with Login_click button.

For example. My first page is MasterPage.Master_Default.aspx. Then I login. After that, I want go to next page, such as MasterPage.Master_Contact.aspx.

Where should I put session code?
0
 
LVL 57

Assisted Solution

by:HainKurt
HainKurt earned 1336 total points
ID: 36498603
after you verified the user, create session variable and transfer it to appropriate page...
on other pages, check session("username") if it is "" transfer user to login page, if it is set then dont do anything (stay on the same page)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Assisted Solution

by:gopaltayde
gopaltayde earned 664 total points
ID: 36516122
may be below link is helpful -
http://msdn.microsoft.com/en-us/library/ff647070.aspx

You can always read the authCookie value to get the user name.
0
 

Author Comment

by:dejandejanovic
ID: 36525070
Helpers,
found it.

This code is from Masterpage.master.vb
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "TEST" Then
            Session("TEST") = TextBox1.Text
        Else
            TextBox1.Text = ""
            Session.Clear()
        End If
    End Sub

    Protected Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
        TextBox1.Text = Session("TEST")
    End Sub

Open in new window


Default.aspx.vb, and other pages
Protected Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
        CType(Master.FindControl("textbox1"), TextBox).Text = Session("TEST")
    End Sub

Open in new window

0
 

Author Closing Comment

by:dejandejanovic
ID: 36525081
Thanks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

752 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