How to get current user logon in windows service


 Hello i want to run  windows service on client machine that   makes an entry of Current user logon  in server database

well i tried three ways to do it

1) Enviroment.username
2) System.security.Principals.windowsidentity.getCurrent();
3) through windows registry


All three methods work well when i use them in console application but  when i make a windows service  the first two returns me name of user who started the service and third method returns me nothing

 Here's my code that is written in my start method

///////////////////////////////////////////////////////////*******************************//////////////////////////////////////////
   SqlConnection con;
   SqlCommand cmd;
   SqlDataReader dr;

  
   
   String str1,str2;
   DateTime dt=DateTime.Now;
 


 
   RegistryKey hkcu=Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Microsoft");
   RegistryKey hkt=hkcu.OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Explorer");
   str2= (string)hkt.GetValue("Logon User Name");

   FileStream fs = new FileStream(@"c:\jk.txt",FileMode.OpenOrCreate, FileAccess.Write);
   StreamWriter SR = new StreamWriter(fs);
   SR.WriteLine(str2);
   SR.Flush();

   str1="insert into Attendence Values('"+ str2 +"','Time In','Time Out','"+dt.ToString()+"','Present','null')";
   
   con= new SqlConnection("Data Source=INSSS9;Initial Catalog=Attentence System;User Id=sa;Password=sa123;" );
   cmd = new SqlCommand(str1,con);
   con.Open();
   dr=cmd.ExecuteReader();
   dr.Close();
   con.Close();

///////////////////////////////////////////////****************************************//////////////////////////////////////////////

 

PLz help meee




Answer this question

How to get current user logon in windows service

  • ajlopez

    hello vikram
       Thanx for replying . I want to make attendance system of a company . So i thought to user windows service bez whenever employee logon on client machine  my service will mark his attendance on server sql database . And i m currently woking in Asp.net . You tell  me what is the best way to achieve this thing

              THank u

  • Radha Krishna Prasad

    the employee use Client or Browse
    and your system is C/S or B/S
    My native language is chinese.some words i not understand nicety

    if you want to mark employee's log in the sql server datebase

  • FrankS_KCDw

    Hi,

    Would you be able to install the service on each machine or just the domain controller   If it is just the domain controller, then there is no accurate way of doing this.

    I did find a script which claims to do the same for a single machine: Note that it also lists other service accounts. Save the file as a .VBS extension and double click on it. Removing Machine name from the query should make it work for domain.

    Set objArgs = WScript.Arguments
    If objArgs.count = 1 Then
      strComputer = objArgs(0)
    Else
      ' Retrieve local computer name.
      Set objWshNet = CreateObject("Wscript.Network")
      strComputer = objWshNet.ComputerName
    End If


    Set objWMI = GetObject("WinMgmts:{impersonationLevel=impersonate}!//" _
                    & strComputer & "/root/cimv2")


    Set colItems = objWMI.ExecQuery _
                    ("Select Name from Win32_UserAccount WHERE Domain = '" _
                    & strComputer & "'",,48)

    For Each objItem In colItems
      Wscript.Echo "User name: " & objItem.Name
    Next

    Regards,
    Vikram



  • Cedric RICARD

    Hi Jahangir,

    This is a very interesting question. There is a problem here because the Windows Service can run as a separate identity and does not even require any user to explicitly logged on to the machine. And on the other end, there might be many users logged on. So who do you consider as the logged on user

    Regards,
    Vikram

  • cmccarrick


    Hey thank u every one
        I got a alternative way to do this 

     
    Thank u ones again

  • Weng_Smith

    Hi Jahangir,
    Can you tell me please the way you did that, I want to do something simillar.

    Regards...

  • How to get current user logon in windows service