Login and FormsAuthentication

Posted on 2006-05-29
Last Modified: 2008-03-17
Can someone explain to me in very lay terms what each line of this code is actually doing (including all the comma-seperated variables).  I'm using it in my login and it works, but I'd actually like to know what is going on.

                                       Dim isPersistent As Boolean = True
                    Dim ticket As FormsAuthenticationTicket = New FormsAuthenticationTicket(1, strUserName.Text, DateTime.Now, DateTime.Now.AddHours(1), isPersistent, strUserName.Text, FormsAuthentication.FormsCookiePath)
                    Dim hash As String = FormsAuthentication.Encrypt(ticket)
                    Dim cookie As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, hash)
                    FormsAuthentication.RedirectFromLoginPage("default.aspx", False)

Question by:davidcahan
    LVL 96

    Accepted Solution

    1) FormAuthenticationTicket:

    Provides access to properties and values of the ticket used with forms authentication to identify users

    Constructor (New) that you are using:

    Public Sub New ( _
          version As Integer, _
          name As String, _
          issueDate As DateTime, _
          expiration As DateTime, _
          isPersistent As Boolean, _
          userData As String _

    The version number of the ticket.

    The user name associated with the ticket.

    The local date and time at which the ticket was issued.

    The local date and time at which the ticket expires.

    true if the ticket will be stored in a persistent cookie (saved across browser sessions); otherwise, false. If the ticket is stored in the URL, this value is ignored.

    The user-specific data to be stored with the ticket.

    2) FormsAuthentication.Encrypt:

    Creates a string containing an encrypted forms-authentication ticket suitable for use in an HTTP cookie.

      3)  This line creates a cookie from the encrypted hash from step #2

           Dim cookie As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, hash)

       It allows you to store the authentication already performed.

    4) FormsAuthentication.RedirectFromLoginPage("default.aspx", False)

       Redirects to the main starting page (default.aspx)

    LVL 27

    Assisted Solution

    1 is the version, strUsername.text is  the username being authenticated, datetime is the cookie expiration slider adding an hour, presistent to remember the user the last is the path where cookie is store
    hash is to encrypt the password
    cookie = the cookie created
    redirect up on successful login

    take a look here


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    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 this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    734 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

    18 Experts available now in Live!

    Get 1:1 Help Now