Getting the current logged on user and NOT the user running the App

G'day

I'm trying to find a way to get the current Username of the person logged on. I ahve tried the following:


Answer this question

Getting the current logged on user and NOT the user running the App

  • Whoisit

    Yes that's it!!!!

    P.S. This code DOES work in Visual Studio 2003 edition, but not in this edition.

    BA

  • Barlet

    You will need to make sure hat you have a reference to the system.management namespace in your application.



  • Wasim

    Apologies if unclear. I'll try again...

    Simple facts:

    • The core windows process named "explorer.exe" is ALWAYS run as the currently logged on user, as far as I'm aware, this cannot be faked easily.
    • Programs (apps) can be run under a different account using the "Run As" function, for example, I could execute the program "Notepad.exe" using the "Run As" option in Task Scheduler and run it as the PC's local Administrator (assuming I also store the password into the Scheduled Task's options)
    • Using the options below, I can only find out the user RUNNING the program, NOT the actual logged on user (meaning (a) this can be faked and (b) this does NOT give a true reading of the user who is logged on)
    The scenario I find myself in...

    I am involved in running a network of 900+ users. I am creating an application which needs to detect who is logged onto any of the 600+ PC's and if the username matches my given criteria, the rest of the program executes, else it quits.

    I want to guarantee that no matter who is logged on the prgram has the necessary right to run (some have very restricted access with mandatory profiles and though they may have permissions to run this particular app, I would prefer to run it as a dedicated account for safety).

    I set up a scheduled task to run the program. Lets say the username I am looking to match is "FredPerry". In my program I use a username match similar to this:

    Dim strCurrentUser As String = Environment.Username
    Dim strCompareName As String = "FredPerry"

    'This check for a username match
    If strCurrentUser = strCompareName Then GoTo SectionTwo Else GoTo Quit

    So when a user logs onto a PC the software runs As a Scheduled Task in the background at regular intervals checking for a user match. If it finds a match it would run "SectionTwo:" otherwise it runs the "Quit:" section.

    I want to run the scheduled task as a specific network account, we'll call it "DOMAIN\AppAccount"

    The problem:

    In the scheduled task I set it to run as "DOMAIN\AppAccount" with the password stored. This account is setup with permission to run the app and not much else in case the password leaks, in which case the account would be useless for most other things.

    When "FredPerry" logs on the app launces silently in the background looking for a match. The problem is, the app is being run as "DOMAIN\AppAccount" not as "FredPerry" which means the string above for strCurrentUser (
    Environment.Username) reads as AppAccount and not the locally logged on user, hence the app Quits even though the correct user is logged on.

    The Solution:

    I need the app to be able to determine, not
    Environment.Username but rather the true logged on user. I'm sure there are many ways to do this but the only one I have found is the search throught the running processes list, find "Explorer.exe" and read the current user from that, as it does not change as long as the user is logged on.

    The problem is, the code which I have been told should work doesn't! The code I have been informed should work is this:

    Dim mc As New ManagementClass("Win32_Process")
    Dim moc As ManagementObjectCollection = mc.GetInstances
    Dim mo As ManagementObject
    Dim processDomain, processUser, outName As String

    For Each mo In moc

    Dim p As New ROOT.CIMV2.Process(mo)
    p.GetOwner(processDomain, processUser)

    If (p.Name.Trim = "explorer.exe") Then
    outName = processUser
    Exit For
    End If

    Next

    The error i get is this:

    Error 1 Type 'ROOT.CIMV2.Process' is not defined.

    Hope this makes more sense, sorry for the waffle!

  • msn

    I've read your question three times and I'm afraid I don't understand the description of the problem.

    Could you post a small snippet of code and identify where the failure occurs



  • dusselim

    So it sounds like what you're looking for is the identity of the interactive (authenticated Windows) user and not the identity of the user under which the process/task is executing. Is this correct



  • tylerperkins36

    Finally got it to work! Code below:

    Dim strCurrentUser As String = ""
    Dim moReturn As Management.ManagementObjectCollection
    Dim moSearch As Management.ManagementObjectSearcher

    Dim mo As Management.ManagementObject

    'This scrolls through all the running processes on the PC to determine who is running the "explorer.exe" process. It then returns the username ready for comparison.
    moSearch = New Management.ManagementObjectSearcher("Select * from Win32_Process")

    moReturn = moSearch.Get


    For Each mo In moReturn

    Dim arOwner(2) As String

    mo.InvokeMethod("GetOwner", arOwner)

    Dim strOut As String

    strOut = String.Format("{0} Owner {1} Domain {2}", mo("Name"), arOwner(0), arOwner(1))

    If (mo("Name") = "explorer.exe") Then

    strCurrentUser = String.Format("{0}", arOwner(0))

    End If

    Next



  • Farhan234

    Glenn Wilson wrote:
    You will need to make sure hat you have a reference to the system.management namespace in your application.

    Got that, but not ROOT.CIMV2.



  • Philip_Bullard

    Any further ideas on this

    BA

  • Bobby Bowden

    Ben Avery wrote:
    Any further ideas on this

    I don't have anything yet. I was looking at it yesterday but the example you posted apparently requires an additional namepace that I didn't have.

    I'm fairly certain it's doable, since the same scenario would apply for a Windows Service, but I'm looking for a relatively straightforward example before I have to get into something rather ugly.



  • HappyTomato

    There is an example on getting process and owners on the vb-tips site.


  • Getting the current logged on user and NOT the user running the App