0

I have an asp.net web application where I want to store user information when a session start in a text file in my pc. For that I use Global.asax and its void Session_Start() method. But when a client access my page over lan, the text file is not created in my pc. Only when I visit my page the file creates. I want my user name, mac address, pages user visited, session start time, session end time in a text file in my pc when clients are accessing my site over lan. I am a beginner at asp.net. So any help will be highly appreciated.

 void Session_Start(object sender, EventArgs e)
    {
        string txtFilePath = @"C:\LogInfo.txt";
        StreamWriter w // ???

        string hostName = 
System.Net.Dns.GetHostEntry(Request.ServerVariables["REMOTE_HOST"]).HostName;
        UserInfo userObj = new UserInfo();
        string macAddress = userObj.GetMACAddress();
        HttpContext context = HttpContext.Current;
        string baseURL = context.Request.Url.AbsolutePath;

        w = File.CreateText(txtFilePath);//File.AppendText(txtFilePath);
        w.Write("Start Log Entry: ");
        w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
            DateTime.Now.ToLongDateString());
        w.WriteLine("\n");
        w.WriteLine("User: {0}", hostName);
        w.WriteLine("\nMAC Address: {0}", macAddress);
        w.WriteLine("-------------------------------\r\n");
        w.Flush();
        w.Close(); 
 }
David
  • 15,894
  • 22
  • 55
  • 66
Reshad
  • 49
  • 1
  • 11
  • AFAIK you can't get a MAC address across HTTP. I guess you could get the Client's LAN IP address and then [ARP](http://technet.microsoft.com/en-us/library/cc961394.aspx) [it](http://stackoverflow.com/questions/1148778/how-do-i-access-arp-protocol-information-through-net), assuming there is no gateway in between. BTW seems an issue with `StreamWriter` above. – StuartLC Dec 11 '13 at 04:37
  • When a client accesses the page over the LAN.. can you set a breakpoint in Session_Start, start debugging, and then verify that the session start method is at least being hit when someone accesses your page ? – sh1rts Dec 11 '13 at 04:48
  • Can you give me a detailed information about how can I get it works? Thanks for your reply. – Reshad Dec 11 '13 at 04:56

1 Answers1

2

You can use :

Request.ServerVariables["LOGON_USER"].ToString(); -- User 
Request.UserHostAddress.ToString(); -- User i/p
Request.PhysicalPath.ToString();    -- Page accessed

You can note the time session started in SESSION_START and the time session ended in SESSION_END methods of global.asax

Regarding pages accessed - In every page, when accessing, you may put code, probably in a base page, to write in file which page was accessed. As multiple pages could be accessed in a session.

Also, make sure there are enough rights to write the file on your machine. Since, you have rights, you are able to write without issues. You need to impersonate your user account, or any account for all users in order to write into files from your machine.

Links about impersonation : http://msdn.microsoft.com/en-us/library/xh507fc5.ASPX http://www.pcreview.co.uk/forums/impersonate-user-asp-net-access-network-file-share-t1343830.html

Also, note, you need to impersonate only to access a certain resource. Not the user to access the system as a whole as you want to record his information.

Regarding mac address - Refer to the following Url - http://www.nullskull.com/a/510/get-mac-address-of-any-machine-from-ip.aspx

Happy coding!!!

Krishna
  • 170
  • 7
  • 1
    Thanks for your reply. I can now create text file on my pc. But I can't get session end time from Session_End() event because it only raises when the session time in web.config completes or I call Session.Abandon(). For that I want to check if the individual page view state is zero (so that the user is not visiting any page) in Session_Start(). But I can't figure it out how to do it. Any help??? – Reshad Dec 11 '13 at 12:10
  • Ideally a session end is indicated when the session (i.e. a period when user is accessing a website) gets timed out (exceeds session time in web.config) or the user hits a log out (where you can include your session.abandon()). You do not have to explicility check whether the user is not viewing any page. – Krishna Dec 12 '13 at 05:42