|
-
Oct 17th, 2002, 05:04 PM
#1
Thread Starter
Addicted Member
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
-
Oct 17th, 2002, 05:06 PM
#2
Try this:
VB Code:
Imports System.Security
Imports System.Security.Principal
Dim id As WindowsIdentity = WindowsIdentity.GetCurrent
Msgbox id.Name 'if they are logged into a domain it returns the domain\username
-
Oct 17th, 2002, 05:28 PM
#3
Thread Starter
Addicted Member
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
-
Oct 17th, 2002, 06:11 PM
#4
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?
-
Oct 17th, 2002, 06:38 PM
#5
Frenzied Member
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
-
Oct 17th, 2002, 06:57 PM
#6
Thread Starter
Addicted Member
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
-
Oct 17th, 2002, 07:37 PM
#7
Thread Starter
Addicted Member
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
-
Oct 17th, 2002, 09:41 PM
#8
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.
-
Oct 18th, 2002, 05:02 AM
#9
Hyperactive Member
How about this?
VB Code:
Shared Function ExtractUser(ByVal pvbooIncludeServer As Boolean) As String
'******************************************
'* Extract the NT User LOGIN Name *
'******************************************
Dim objMyIdentity As System.Security.Principal.WindowsIdentity
Dim objMyPrincipal As System.Security.Principal.WindowsPrincipal
Dim strUser As String
Dim intLastSlashIndex As Int32
objMyIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
objMyPrincipal = New System.Security.Principal.WindowsPrincipal(objMyIdentity)
strUser = String.Format("Logon name: {0}", objMyIdentity.Name)
If Not pvbooIncludeServer Then
If strUser <> "" Then
intLastSlashIndex = strUser.LastIndexOf("\")
If intLastSlashIndex <> -1 Then
strUser = strUser.Substring(intLastSlashIndex + 1)
End If
End If
End If
Return strUser
End Function
-
Oct 18th, 2002, 08:03 AM
#10
Thread Starter
Addicted Member
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
-
Oct 19th, 2002, 04:36 PM
#11
New Member
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.
-
Oct 20th, 2002, 07:22 AM
#12
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|