Solved

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

Posted on 2011-09-07
6
297 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now