Remember me next time - how to use - VB.NET

Discussion in 'Software' started by viki, Jul 10, 2009.

  1. viki

    viki

    Joined:
    May 26, 2009
    Messages:
    4
    Hi

    I am new to .NET and i have problem in using login control. I made a simple login page and it works fine but i dont know how to use the remember me next time option. can anyone please help me with some piece of code or links..

    Thanks in Advance.
    Viki
    viki, Jul 10, 2009
    #1
    1. Advertising

  2. viki

    viki

    Joined:
    May 26, 2009
    Messages:
    4
    solution for Remember me next time in Login Control using VB.NET

    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Data.SqlClient

    Partial Class login
    Inherits System.Web.UI.Page

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    If Not IsPostBack Then
    If Request.Cookies("myCookie") IsNot Nothing Then
    Dim cookie As HttpCookie = Request.Cookies.[Get]("myCookie")
    Login1.UserName = cookie.Values("username")
    Login1.RememberMeSet = (Not [String].IsNullOrEmpty(Login1.UserName))
    End If
    Dim txtUser As TextBox = TryCast(Login1.FindControl("UserName"), TextBox)
    If txtUser IsNot Nothing Then
    Me.SetFocus(txtUser)
    End If
    End If
    Response.Cache.SetNoStore()

    End Sub


    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As AuthenticateEventArgs)
    If YourValidationFunction(Login1.UserName, Login1.Password) Then
    e.Authenticated = True
    End If
    End Sub

    Protected Sub Login1_LoginError(ByVal sender As Object, ByVal e As EventArgs)
    If ViewState("LoginErrors") Is Nothing Then
    ViewState("LoginErrors") = 0
    End If

    Dim ErrorCount As Integer = CInt(ViewState("LoginErrors")) + 1
    ViewState("LoginErrors") = ErrorCount

    If (ErrorCount > 3) AndAlso (Login1.PasswordRecoveryUrl <> String.Empty) Then
    Response.Redirect(Login1.PasswordRecoveryUrl)
    End If
    End Sub

    Private Function YourValidationFunction(ByVal UserName As String, ByVal Password As String) As Boolean
    Dim boolReturnValue As Boolean = False
    Dim strConnection As String = "Data Source=SELVANOTEBOOK\MSSQLSERVER2008;Initial Catalog=AgentLoginDB;Integrated Security=SSPI;"
    Dim sqlConnection As New SqlConnection(strConnection)
    Dim SQLQuery As String = "SELECT username, password FROM userdatabase where username='" & UserName & "' and password = '" & Password & "'"
    Session("username") = UserName
    Dim command As New SqlCommand(SQLQuery, sqlConnection)
    Dim Dr As SqlDataReader
    sqlConnection.Open()
    Dr = command.ExecuteReader()

    While Dr.Read()
    If (UserName = Dr("UserName").ToString()) And (Password = Dr("Password").ToString()) Then
    boolReturnValue = True
    End If
    Dr.Close()
    Return boolReturnValue
    End While
    Return boolReturnValue
    End Function


    Protected Sub Login1_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login1.LoggedIn

    Dim myCookie As New HttpCookie("myCookie")
    Dim remember As [Boolean] = Login1.RememberMeSet

    If remember Then
    Dim persistDays As Int32 = 15
    myCookie.Values.Add("username", Login1.UserName)
    'you can add years and months too here
    myCookie.Expires = DateTime.Now.AddDays(persistDays)
    Else
    myCookie.Values.Add("username", String.Empty)
    ' overwrite empty string is safest
    'you can add years and months too here
    myCookie.Expires = DateTime.Now.AddMinutes(5)
    End If

    Response.Cookies.Add(myCookie)


    End Sub


    End Class
    viki, Jul 15, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cowboy \(Gregory A. Beamer\)

    Take MCSD.NET with VB.NET and C#.NET in the same time?

    Cowboy \(Gregory A. Beamer\), Jan 1, 2004, in forum: MCSD
    Replies:
    1
    Views:
    485
    The Fox
    Jan 1, 2004
  2. peter
    Replies:
    2
    Views:
    493
    AZ Nomad
    Mar 17, 2006
  3. !!! THIS TIME NEXT YEAR YOU COULD BE A MAIL ORDER

    !!! THIS TIME NEXT YEAR YOU COULD BE A MAIL ORDER MILLIONAIRE !!!

    !!! THIS TIME NEXT YEAR YOU COULD BE A MAIL ORDER, Nov 18, 2006, in forum: MCSE
    Replies:
    3
    Views:
    739
    BD[MCNGP]
    Nov 19, 2006
  4. Alan Browne

    Next (the other next) Gen "DVD" storage

    Alan Browne, May 27, 2005, in forum: Digital Photography
    Replies:
    13
    Views:
    487
    Iain Laskey
    May 31, 2005
  5. london1919
    Replies:
    1
    Views:
    2,924
    Lawrence Garvin \(MVP\)
    Dec 8, 2008
Loading...

Share This Page