WindowsIdentity.GetCurrent().Name result

Posted by Tobi under C# on 6/12/2013 | Points: 10 | Views : 2590 | Status : [Member] | Replies : 3
hi,
i want to write the user logged in name in a file.
im logged in with the user name user but when i use Environnement.username or WindowsIdentity.GetCurrent().Name , it returns me system and not user
in the others forum they say that if this instruction is in a windows service, it returns the user who launched the current thread or process.
i would like to get the user logged in a not the thred launcher.
please help me
thanks




Responses

Posted by: Cherishma on: 6/13/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,
It Returns a WindowsIdentity object that represents the current Windows user.

for eg:

IntPtr accountToken = WindowsIdentity.GetCurrent().Token;
Console.WriteLine( "Token number is: " + accountToken.ToString());
or

Response.Write(" " + WindowsIdentity.GetCurrent().Name.ToString());
o/p: we will get Windows UserName



Tobi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tobi on: 7/10/2013 [Member] Starter | Points: 25

Up
0
Down
thanks for your answear
the problem is that i would like to write a windows service (localsystem) launched by the system not the user
but when the user is connected i would like to get his username.
when i use WindowsIdentity.GetCurrent().Name.ToString()) it returns "system" because the service is launched by the system then i can't get the user connected name
i think that my first service has to launch another user service and this service will get the user name.
please help????

Tobi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tobi on: 8/5/2013 [Member] Starter | Points: 25

Up
0
Down
hello
for persons who are interested by this i finally resolved this probleme using wmi in c#
here is my code:

private string CurrentUserName()

{
var s = new ManagementObjectSearcher ("SELECT UserName FROM Win32_ComputerSystem");

string str = string.Empty;

foreach (ManagementObject moobj in s.Get())
{
if (moobj["UserName"] != null)
{
str = moobj["UserName"].ToString();
}
}

return str;
}


after you just have to use the function CurrentUserName() to get the connected user with a localsystem windows service

Tobi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response