Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-07
6
Medium Priority
?
306 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 60

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 60

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
Industry Leaders: 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 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