• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

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

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
dejandejanovic
Asked:
dejandejanovic
  • 3
  • 2
3 Solutions
 
HainKurtSr. System AnalystCommented:
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
 
dejandejanovicAuthor Commented:
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
 
HainKurtSr. System AnalystCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
gopaltaydeCommented:
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
 
dejandejanovicAuthor Commented:
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
 
dejandejanovicAuthor Commented:
Thanks.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now