Need Help

Discussion in 'General Computer Support' started by viki, Jul 8, 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 8, 2009
    #1
    1. Advertising

  2. viki

    viki

    Joined:
    May 26, 2009
    Messages:
    4
    solution - Remember me next time in Login control

    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 22, 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. Bob
    Replies:
    4
    Views:
    416
    Jacky
    Feb 4, 2004
  2. nuorigin@gmail.com

    HELP - need a cam that doesn't need software install

    nuorigin@gmail.com, Nov 23, 2005, in forum: Digital Photography
    Replies:
    27
    Views:
    587
    Michael Meissner
    Nov 29, 2005
  3. hedayatniac@gmail.com

    re_---need help Network Adapters!!!! NEED HELP!!!!

    hedayatniac@gmail.com, Aug 12, 2006, in forum: Computer Support
    Replies:
    4
    Views:
    651
    hedayatniac@gmail.com
    Aug 13, 2006
  4. Piper
    Replies:
    4
    Views:
    422
    Leythos
    Jan 1, 2007
  5. sinu
    Replies:
    3
    Views:
    472
    and3w
    Jan 19, 2007
Loading...

Share This Page