Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Current Logined user name

Status
Not open for further replies.

FurqanAhmed

Programmer
Oct 1, 2001
87
PK
Hi All,

i am retreaving user name from windows service by using the following two methods

string usr = Environment.UserName;
string usr = System.Windows.Forms.SystemInformation.UserName;

the string "SYSTEM" is returned from both methods, but i want to get the name of the user who is currently logged In.

please anyone know, how to get the current user of the system, so please let me know

Best Regards


YEH GHAZI YEH TERAY PUR-ASRAR BANDAY
 
In the IIS virtual directory properties, under Directory Security make sure 'Anonymous access' is disabled and 'Integrated Windows authentication' is enabled. The following code will then write out the user's username in the format DOMAIN\username:

Code:
System.Security.Principal.WindowsIdentity user = 
     System.Security.Principal.WindowsIdentity.GetCurrent();
Response.Write(user.Name);
 
I do not think it is possible to obtain the current logged user name within a windows service since a windows service can run even when there's no one logged in. The user you will obtain by the methods you have specified ( SYSTEM ) is the user which appears in the service's property pages, under the "Log on" tab.
 
I have also tried:
System.Security.Principal.WindowsIdentity.GetCurrent().Name;
it also gives the same user name which is "SYSTEM".

yes it is true that windows service run even when there's no one logged, how can we get the current logged user of the system, obviously if no one logged , it would return nothing.

Regards

YEH GHAZI YEH TERAY PUR-ASRAR BANDAY
 
Try something like that:
Code:
	class Class1
	{
		[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
		public struct WKSTA_USER_INFO_1
		{
			public string wkui1_username;  
			public string wkui1_logon_domain;  
			public string wkui1_oth_domains;  
			public string wkui1_logon_server;
		}

		[DllImport("netapi32.dll", CharSet=CharSet.Unicode)]
		extern static uint NetWkstaUserEnum( string servername,
		uint level,
		[Out] out WKSTA_USER_INFO_1[] bufptr,
		uint prefmaxlen,
		out uint entriesread,
		out uint totalentries,
		[In, Out] IntPtr resumehandle );

		[STAThread]
		static void Main(string[] args)
		{
			try
			{
				WKSTA_USER_INFO_1[] buf = new WKSTA_USER_INFO_1[]{};
				uint entriesread;
				uint totalentries;
				uint res = NetWkstaUserEnum(null, 1, out buf, 
					uint.MaxValue,out entriesread, 
					out totalentries, IntPtr.Zero);
				if( res != 0 )
				{
					throw new System.ComponentModel.Win32Exception((int)res);
				}
				foreach( WKSTA_USER_INFO_1 entry in buf )
				{
					Console.WriteLine("LoggedIn user: {0}@{1}",
						entry.wkui1_username,
						entry.wkui1_logon_domain);
				}
			}
			catch(Exception e)
			{
				Console.WriteLine("ERROR: {0}",e.ToString());
			}

		}
	}

Cheers,
Batvanko ;-)
 
B00gyeMan has it right --
Services start before anyone logs in, so seeing SYSTEM is correct.

If you look in the services control manager, you'll see the account the service is using is probably set to LocalSystem. You can change this to be any other authorized user, and the service will run as that user.

But it does *not* run as whoever the current logged-in user is.

What if this were true? This would mean that services such as MS-SQLServer wouldn't start until someone logged in, and then would stop when that person logged out. Or... if a person with low priviledge logs in, the database engine might not have permission to read it's database files.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top