Solved

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

Posted on 2011-09-07
6
299 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
  • 3
  • 2
6 Comments
 
LVL 51

Accepted Solution

by:
HainKurt earned 334 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 51

Assisted Solution

by:HainKurt
HainKurt earned 334 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 7

Assisted Solution

by:gopaltayde
gopaltayde earned 166 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

806 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