title: asp.net vb2010 How to log Windows Authentication events - i.e. save user name and login time
body: <p>Just now getting into the asp.net environment with VB 2010. I have designed an intranet page that we will be using to auction off old equipment to employees. I've had some very good luck getting the whole thing working, but I have hit a snag on one minor issue: I would like to keep a database table of user logins and times.</p>
  
  <p>Several methods have presented themselves, but so far none of them have been adequate. The main problem seems to be that regardless of the event I use to record the login, they fire multiple times resulting in as many as 4 or 5 entries in the login database for the same login event, not to mention additional times when something on the page changes for whatever reason.</p>
  
  <p>The system uses Windows Authentication, so the user must login with his/her company credentials.</p>
  
  <p>I can't remember what all I have tried, but the first thing I tried was the <code>Page_Load</code> event because it seemed obvious. I've learned that because the page updates the display every ten seconds, <code>Page_Load</code> event fires numerous times throughout the session.</p>
  
  <p>Fighting my way through other ideas, all of which failed, I came to the <code>WindowsAuthentication_OnAuthenticate</code> event in the <code>Global_asax</code> class, shown below:</p>
  
  <pre><code>Imports System.Web.SessionState
  Imports System.Data.SqlClient
  Imports System.Web
  Imports System.Web.UI.WebControls
  --------------------------------------------
  Public Sub WindowsAuthentication_OnAuthenticate(ByVal sender As Object, ByVal args As WindowsAuthenticationEventArgs)
  
          Dim strLoginName = args.Identity.Name
          Dim sqlCommandString As String = "INSERT into tblLogin (UserLogin,DateTime) VALUES ('" &amp; _
                  strLoginName &amp; "','" &amp; _
                  Format(Now, "MMM dd, yyyy hh:mm:ss") &amp; "');"
          Dim sqlConn As New SqlConnection(strConnectionString)
          Dim sqlCommand As SqlCommand = New SqlCommand(sqlCommandString, sqlConn)
          sqlCommand.Connection.Open()
          sqlCommand.ExecuteNonQuery()
          sqlCommand.Connection.Close()
  
  End Sub
  </code></pre>
  
  <p>which also seems to fire multiple times. I then tried to work a way where setting a Boolean value to TRUE upon authentication so that the data would be written to the database only once:</p>
  
  <pre><code>    Public Sub WindowsAuthentication_OnAuthenticate(ByVal sender As Object, ByVal args As WindowsAuthenticationEventArgs)
  
      If Not boolUserIsLoggedIn Then
          boolUserIsLoggedIn = True
  
          Dim strLoginName = args.Identity.Name
          Dim sqlCommandString As String = "INSERT into tblLogin (UserLogin,DateTime) VALUES ('" &amp; _
                  strLoginName &amp; "','" &amp; _
                  Format(Now, "MMM dd, yyyy hh:mm:ss") &amp; "');"
          Dim sqlConn As New SqlConnection(strConnectionString)
          Dim sqlCommand As SqlCommand = New SqlCommand(sqlCommandString, sqlConn)
          sqlCommand.Connection.Open()
          sqlCommand.ExecuteNonQuery()
          sqlCommand.Connection.Close()
      End If
  
  End Sub
  </code></pre>
  
  <p>and subsequent authentication iterations would skip the database write code; but then I needed to find a way to clear the Boolean value when the user logged off or closed the session. I couldn't figure out how to trap a "Logged Off" event, and the <code>Session_End</code> and <code>Application_End</code> events didn't work as I had hoped.</p>
  
  <p>I feel like this is getting too convoluted to make sense. To put it simply, I just need a way to record a user's login name and date only once per session. Can anyone help?</p>
  
  <p>[Edit: Although this is not really an essential element of the page, it has become a crusade for me to get it to work if not for any other reason than to understand the process.]</p>