Results 1 to 12 of 12

Thread: UserName

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207

    UserName

    Does anyone know how to get the username of the current logged on user from within a service. Apprently you can not use Environment.Username because that will simply return "NT Authority" because the service is running under the localservice user.

    Any help would be greatly appreciated.

    Jeremy

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Try this:
    VB Code:
    1. Imports System.Security
    2. Imports System.Security.Principal
    3.  
    4. Dim id As WindowsIdentity = WindowsIdentity.GetCurrent
    5. Msgbox id.Name 'if they are logged into a domain it returns the domain\username

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    The only problem is that if the program is running as a service the user comes back as this: NT AUTHORITY\SYSTEM.

    I am actually interested in the user sitting at the keyboard.

    Jeremy

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Well unless you pass in the identity or the token then I don't think that will work with a service, because the service is logged in before the user as system. I was hoping GetCurrent would grab the user but I guess not from a service. What is it you are trying to do with the name?

  5. #5
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    What OS are you guys using? I tried using Environment.UserName and it returned the name that I logged on with.

    BTW I'm using XP Pro.
    Dont gain the world and lose your soul

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    Acutally all i need to do is find out what it is. Basically what I have to do is find out what/or if a user is logged into a computer, and then the service will need to send the username across the network to a server and the server will write it to a file.

    I am using XP Pro as well but the service does not return the correct username, when run as a regular program it will (mainly because when it is a regular program the user is running it, when it is a service the local service account is running it).

    You would think there would be a way to find out if a user is logged into the computer.

    Jeremy

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    Ok,

    I am thinking of going about this in a different way, maybee. If you bring up the talk manager in windows 2000 or xp it will show you a list of processes that are running them. I can easily create a list of the processes but does anyone know how to find out who is running that process, I then could just look down through the list and see if there are any of them not running by the system.

    Jeremy

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    The VB.NET help files mentions a GetLogon API function that returns a token with which you can create a WindowsIndentity object. I don't have any details about it but maybe that would return the logged in user.

  9. #9
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    How about this?

    VB Code:
    1. Shared Function ExtractUser(ByVal pvbooIncludeServer As Boolean) As String
    2.       '******************************************
    3.       '* Extract the NT User LOGIN Name         *
    4.       '******************************************
    5.       Dim objMyIdentity As System.Security.Principal.WindowsIdentity
    6.       Dim objMyPrincipal As System.Security.Principal.WindowsPrincipal
    7.       Dim strUser As String
    8.       Dim intLastSlashIndex As Int32
    9.  
    10.       objMyIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
    11.       objMyPrincipal = New System.Security.Principal.WindowsPrincipal(objMyIdentity)
    12.       strUser = String.Format("Logon name: {0}", objMyIdentity.Name)
    13.       If Not pvbooIncludeServer Then
    14.          If strUser <> "" Then
    15.             intLastSlashIndex = strUser.LastIndexOf("\")
    16.             If intLastSlashIndex <> -1 Then
    17.                strUser = strUser.Substring(intLastSlashIndex + 1)
    18.             End If
    19.          End If
    20.       End If
    21.       Return strUser
    22.  
    23.    End Function

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    Nope that that function will only return the NT Authority\System User also. I appears that everything I try to do will only access the system properties of the System User, I am starting to wonder if it is possible to access the properties of another logged in user.

    Jeremy

  11. #11
    New Member
    Join Date
    Oct 2002
    Posts
    1
    I have a similar problem, maybe someone has an idea: I have to run (execute) an application within a service but I want to switch the owner of the application from System to current logged user ...

    I really don't care if an authentication dialog appears, just a way to get within a service some user specific resources.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    What I have found out is this. It is not possible to interact with another desktop from the .net framework. Microsoft claims that you will have to use unmanaged code to accomplish this.

    Here is the artical and the reference for the msdn website: ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vbcon/html/vbconintroductiontontserviceapplications.htm

    Jeremy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width