Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Windows Identity of the currently logged in user

Windows Identity of the currently logged in user

Scheduled Pinned Locked Moved C#
databasesysadminsecurityhelpquestion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ramz_g
    wrote on last edited by
    #1

    Hi, I have created an application which runs on a Web Server. This web server in-turn runs as a windows service. This windows service's account is configured as follows

    ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();

    serviceProcessInstaller.Account = ServiceAccount.LocalSystem;

    Now I have a scenario in which I have to access a database and execute queries in it which needs the currently logged in user. The problem is that whenever I try to use

    System.Security.Principal.WindowsIdentity.GetCurrent().Name

    I am getting the name as "SYSTEM" and not the logged in user's name. And I know that this is because the service is run at the system level. I can change the ServiceAccount as

    serviceProcessInstaller.Account = ServiceAccount.User

    I can now get the currently logged in user name. But, this requires the user to enter his user name and password when the service is being installed and I do not want that to happen. Hence is there any other way in which I can get the WindowsIdentity object of the currently logged in user? Please suggest.. Thanks, ramz_g

    L 2 Replies Last reply
    0
    • R ramz_g

      Hi, I have created an application which runs on a Web Server. This web server in-turn runs as a windows service. This windows service's account is configured as follows

      ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();

      serviceProcessInstaller.Account = ServiceAccount.LocalSystem;

      Now I have a scenario in which I have to access a database and execute queries in it which needs the currently logged in user. The problem is that whenever I try to use

      System.Security.Principal.WindowsIdentity.GetCurrent().Name

      I am getting the name as "SYSTEM" and not the logged in user's name. And I know that this is because the service is run at the system level. I can change the ServiceAccount as

      serviceProcessInstaller.Account = ServiceAccount.User

      I can now get the currently logged in user name. But, this requires the user to enter his user name and password when the service is being installed and I do not want that to happen. Hence is there any other way in which I can get the WindowsIdentity object of the currently logged in user? Please suggest.. Thanks, ramz_g

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      http://msdn.microsoft.com/en-us/library/system.environment.username.aspx[^]

      R 1 Reply Last reply
      0
      • L Lost User

        http://msdn.microsoft.com/en-us/library/system.environment.username.aspx[^]

        R Offline
        R Offline
        ramz_g
        wrote on last edited by
        #3

        Hi stancrm, Thanks for your reply.. But I have tried Environment.UserName already but it doesn't help, maybe because my service is running at the System level and the environment here is the System and not the user. Also, I would like to know whether there is any class that actually returns me the Windows Identity object of a specified user. Thanks, ramz_g

        1 Reply Last reply
        0
        • R ramz_g

          Hi, I have created an application which runs on a Web Server. This web server in-turn runs as a windows service. This windows service's account is configured as follows

          ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();

          serviceProcessInstaller.Account = ServiceAccount.LocalSystem;

          Now I have a scenario in which I have to access a database and execute queries in it which needs the currently logged in user. The problem is that whenever I try to use

          System.Security.Principal.WindowsIdentity.GetCurrent().Name

          I am getting the name as "SYSTEM" and not the logged in user's name. And I know that this is because the service is run at the system level. I can change the ServiceAccount as

          serviceProcessInstaller.Account = ServiceAccount.User

          I can now get the currently logged in user name. But, this requires the user to enter his user name and password when the service is being installed and I do not want that to happen. Hence is there any other way in which I can get the WindowsIdentity object of the currently logged in user? Please suggest.. Thanks, ramz_g

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          ramz_g wrote:

          System.Security.Principal.WindowsIdentity.GetCurrent().Name I am getting the name as "SYSTEM" and not the logged in user's name. And I know that this is because the service is run at the system level.

          That's correct. Your service is running with the privileges of the system account.

          ramz_g wrote:

          I can now get the currently logged in user name. But, this requires the user to enter his user name and password when the service is being installed and I do not want that to happen.

          It would be a breach in security if you could impersonate that user without them entering a password for their account. Either your database needs to grant access to the builtin system-account, or the service needs to run under it's own account, or you use the users' account. If you're using SQL Server, then you might want to switch to a SQL-login (as compared to Windows Authentication). You wouldn't want to do that with the 'sa' user, but it would get you started :)

          I are Troll :suss:

          R 1 Reply Last reply
          0
          • L Lost User

            ramz_g wrote:

            System.Security.Principal.WindowsIdentity.GetCurrent().Name I am getting the name as "SYSTEM" and not the logged in user's name. And I know that this is because the service is run at the system level.

            That's correct. Your service is running with the privileges of the system account.

            ramz_g wrote:

            I can now get the currently logged in user name. But, this requires the user to enter his user name and password when the service is being installed and I do not want that to happen.

            It would be a breach in security if you could impersonate that user without them entering a password for their account. Either your database needs to grant access to the builtin system-account, or the service needs to run under it's own account, or you use the users' account. If you're using SQL Server, then you might want to switch to a SQL-login (as compared to Windows Authentication). You wouldn't want to do that with the 'sa' user, but it would get you started :)

            I are Troll :suss:

            R Offline
            R Offline
            ramz_g
            wrote on last edited by
            #5

            Hi Eddy, Thanks a lot for your reply..

            Eddy Vluggen wrote:

            It would be a breach in security if you could impersonate that user without them entering a password for their account.

            Yes I understand that.

            Eddy Vluggen wrote:

            If you're using SQL Server, then you might want to switch to a SQL-login

            No, I'm not using SQL Server. All I'm using is a simple Sqlite database and I'm using simple queries where I have to use the windows logged-in user name. So, is there any way that I can obtain the logged in user name alone? Thanks, ramz_g

            L 1 Reply Last reply
            0
            • R ramz_g

              Hi Eddy, Thanks a lot for your reply..

              Eddy Vluggen wrote:

              It would be a breach in security if you could impersonate that user without them entering a password for their account.

              Yes I understand that.

              Eddy Vluggen wrote:

              If you're using SQL Server, then you might want to switch to a SQL-login

              No, I'm not using SQL Server. All I'm using is a simple Sqlite database and I'm using simple queries where I have to use the windows logged-in user name. So, is there any way that I can obtain the logged in user name alone? Thanks, ramz_g

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              ramz_g wrote:

              All I'm using is a simple Sqlite database and I'm using simple queries where I have to use the windows logged-in user name.

              A service is started before a user logs in to the system, and there might be more than a single user signed in. The simplest way would be to create a small application that writes the username to a file, and to drop it in the Startup-folder. Alternatively, you might try WMI[^].

              I are Troll :suss:

              R 1 Reply Last reply
              0
              • L Lost User

                ramz_g wrote:

                All I'm using is a simple Sqlite database and I'm using simple queries where I have to use the windows logged-in user name.

                A service is started before a user logs in to the system, and there might be more than a single user signed in. The simplest way would be to create a small application that writes the username to a file, and to drop it in the Startup-folder. Alternatively, you might try WMI[^].

                I are Troll :suss:

                R Offline
                R Offline
                ramz_g
                wrote on last edited by
                #7

                Hi Eddy, Thanks a lot for the replies..

                Eddy Vluggen wrote:

                The simplest way would be to create a small application that writes the username to a file, and to drop it in the Startup-folder.

                I shall use this... Thanks, ramz_g

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups