Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

How to Track User Login

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mel - 15 Aug 2007 15:54 GMT
I am using "windows" authentication mode.  I would like to store the
username and various information when the user logs on to the website.
Any ideas?

It would be a bonus to store the logout time too but I hear that is
difficult and unreliable but if anyone knows a way to do that too I am
all ears.

(Visual Studio 2005, Asp.net 2.0, Visual Basic)
Ladislav Mrnka - 15 Aug 2007 16:14 GMT
Hi Mel,
if you are using windows authentication IIS is responible for authenticating
your user. Do you allow anonymous users to access your application? If no you
can handle your login in Session_Start. Handling logout is difficult because
if user closes browser you do not receive any information about it (unless
you build some mechanism to ping application from browser in short
intervals). You also can fully believe to Session_End because it is raised
only if session is running InProc = locally in asp.net worker process.

Regards,
Ladislav

> I am using "windows" authentication mode.  I would like to store the
> username and various information when the user logs on to the website.
[quoted text clipped - 5 lines]
>
> (Visual Studio 2005, Asp.net 2.0, Visual Basic)
Ladislav Mrnka - 15 Aug 2007 16:16 GMT
*You also cannot believe to Session_End ...

> Hi Mel,
> if you are using windows authentication IIS is responible for authenticating
[quoted text clipped - 17 lines]
> >
> > (Visual Studio 2005, Asp.net 2.0, Visual Basic)
Mel - 15 Aug 2007 16:42 GMT
On Aug 15, 10:14 am, Ladislav Mrnka
<LadislavMr...@discussions.microsoft.com> wrote:
> Hi Mel,
> if you are using windows authentication IIS is responible for authenticating
[quoted text clipped - 17 lines]
>
> > (Visual Studio 2005, Asp.net 2.0, Visual Basic)

Cool.  I will just add a Global.asax to my project and try adding my
code to the Session_Start procedure.  I'll post the code here when I
get it finished.
Mel - 15 Aug 2007 22:19 GMT
> On Aug 15, 10:14 am, Ladislav Mrnka
>
[quoted text clipped - 24 lines]
> code to the Session_Start procedure.  I'll post the code here when I
> get it finished.

I successfully added the Global.asax as a new item in my Visual Studio
2005 web project.  I set some session variables and then store the
user, company and session id in an access database table called "Web
User Log".  I was not successful in setting the session end time; it
just wasn't possible because my session state mode is not set to
InProc so I dismissed the idea.  Here is the code if anyone is
interested....
Thanks to everyone for your help.

   Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
       ' Code that runs when a new session is started
       Session("IsDeveloper") = False

       'Set session variables
       If Not
IsNothing(Context.User.Identity.Name.Substring(Context.User.Identity.Name.LastIndexOf("\")
+ 1)) Then
           Session("CurUser") =
Context.User.Identity.Name.Substring(Context.User.Identity.Name.LastIndexOf("\")
+ 1)
           Dim CurDom As String = "DC=mydomain, DC=local"
           Dim LdapSvr As String = "mydomain.local"
           Dim LdapPath As String = "LDAP://" & LdapSvr & "/" &
CurDom & ""
           Dim DirEnt As DirectoryEntry = New
DirectoryEntry(LdapPath)
           Dim ds As DirectorySearcher = New
DirectorySearcher(DirEnt, "(sAMAccountName=" & Session("CurUser") &
")")
           ds.PropertiesToLoad.Add("mail")
           ds.PropertiesToLoad.Add("sn")
           ds.PropertiesToLoad.Add("givenName")
           ds.PropertiesToLoad.Add("company")
           ds.PropertiesToLoad.Add("st")
           ds.PropertiesToLoad.Add("l")
           ds.PropertiesToLoad.Add("department")

           Dim sr As SearchResult = ds.FindOne
           Session("SN") =
sr.GetDirectoryEntry().Properties("sn").Value
           Session("GivenName") =
sr.GetDirectoryEntry().Properties("givenName").Value
           Session("mail") =
sr.GetDirectoryEntry().Properties("mail").Value
           Session("Company") =
sr.GetDirectoryEntry().Properties("company").Value
           Session("St") =
sr.GetDirectoryEntry().Properties("st").Value
           Session("city") =
sr.GetDirectoryEntry().Properties("l").Value
           Session("department") =
sr.GetDirectoryEntry().Properties("department").Value
       End If

       If UCase(Session("CurUser")) = "DEVDUDE1" Or
UCase(Session("CurUser")) = "DEVDUDE2" Then
           'don't store login date/time for the web developers who
repeatedly log in and out while debugging.
           Session("IsDeveloper") = True
       Else
           If Not Session("CurUser") Is Nothing Then
               'now writing user name and login time to the WebBMQ
database Web User Log table upon new session
               Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.
4.0;Data Source=\myserver\webbmq.mdb;"
               Dim conWebOrdNum As New
System.Data.OleDb.OleDbConnection(strConn)
               Dim strIns As String = "INSERT INTO [Web User Log]
([User],[Company],[Date/Time of Entry], [Session ID]) VALUES
(?,?,?,?)"
               Dim cmdIns As New
System.Data.OleDb.OleDbCommand(strIns, conWebOrdNum)
               conWebOrdNum.Open()
               If Session("CurUser") Is Nothing Then
               Else
                   cmdIns.Parameters.AddWithValue("User",
Session("CurUser").ToString)
               End If

               If Session("Company") Is Nothing Then
               Else
                   cmdIns.Parameters.AddWithValue("Company",
Session("Company").ToString)
               End If

               If Session("CurUser") Is Nothing And
Session("Company") Is Nothing Then
               Else
                   cmdIns.Parameters.AddWithValue("Date/Time of
Entry", Now.ToString)
                   cmdIns.Parameters.AddWithValue("Session ID",
Session.SessionID.ToString)
               End If
               cmdIns.ExecuteNonQuery()
               conWebOrdNum.Close()
           End If
       End If
   End Sub
Steve - 20 Aug 2007 15:44 GMT
You can put that into the Session_Start event in the Global.asax. In
fact, I don't see why you can't put the logout code in the Session_End
event. The logout time may not be accurate to the minute, because of the
session timeout, but you can get it pretty close...

Steve C.
MCAD,MCSE,MCP+I,CNE,CNA,CCNA

> I am using "windows" authentication mode.  I would like to store the
> username and various information when the user logs on to the website.
[quoted text clipped - 5 lines]
>
> (Visual Studio 2005, Asp.net 2.0, Visual Basic)
Dan Colgan - 04 Sep 2007 14:31 GMT
Steve C... If the Session_End never ever ever ever fires what is the
reason? - I've not ever been able to get it to fire and my Sessionstate
mode is "Inproc"

So far I see no validity in saying you can rely on Session_End since no
one on the web has been able to answer this question consistently.
George Ter-Saakov - 04 Sep 2007 15:43 GMT
Never say never.
How do you know that it's never ever fires?
It does always fire but if you have a bug in the code for Session_End then
it will quietly throw an exception and be caught by ASP.NET without you
knowing. Like for example if you tried to use Response or Request objects in
Session_End code.

Might have a trace in NT Event log though...

George.

> Steve C... If the Session_End never ever ever ever fires what is the
> reason? - I've not ever been able to get it to fire and my Sessionstate
[quoted text clipped - 4 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***
bruce barker - 04 Sep 2007 16:11 GMT
you can rely on session_end if you use inproc, and there are no recyle
events. check your event log for recycles.

-- bruce (sqlwork.com)

> Steve C... If the Session_End never ever ever ever fires what is the
> reason? - I've not ever been able to get it to fire and my Sessionstate
[quoted text clipped - 4 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***
Dan Colgan - 04 Sep 2007 16:41 GMT
Here's the code - you tell me why it doesn't fire

<SCRIPT LANGUAGE="VBScript" RUNAT="Server">

Const strLogFilePath= "C:\Inetpub\wwwroot\AOPCTraining\session.log"

Sub Application_OnStart
    Application("Start") = Now()
End Sub

Sub Session_OnStart
    Session.Timeout = 1
    Session("Start") = Now()

    Dim objFSO, objFile
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strLogFilePath, 8, True)
    Set objFSO = Nothing
    objFile.WriteLine "Session: " & Session.SessionID & " started at " &
Now ()
    objFile.Close
    Set objFile = Nothing
End Sub

Sub Session_OnEnd
    Dim objFSO, objFile
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strLogFilePath, 8, True)
    Set objFSO = Nothing
    objFile.WriteLine "Session: " & Session.SessionID & " ended at " & Now
()
    objFile.Close
    Set objFile = Nothing
End Sub

Sub Application_OnEnd
End Sub

</SCRIPT>
George Ter-Saakov - 04 Sep 2007 16:47 GMT
I see, I thought you talking about ASP.NET not old asp.
It's known issue with plain ASP.

Also writing into file concurrently is not a good idea,
George.

> Here's the code - you tell me why it doesn't fire
>
[quoted text clipped - 37 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***
Dan Colgan - 04 Sep 2007 16:59 GMT
This same exact code is in my global.asax file as well and it doesn't
work there either. ASP or ASP.net doesn't seem to matter, it just
doesn't work.

It seems puzzling to me why the code which is almost word for word in
the session_start doesn't work in the session_end? -

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.