Name of user running thread

  • Thread starter Thread starter Mats-Lennart Hansson
  • Start date Start date
M

Mats-Lennart Hansson

Hi,
I want to get the name of the owner of a thread. I have a component that can
be called from many different components so I want to trace which user that
owns the thread. Is this possible in c#?

Thanks,
Mats-Lennart
 
Hi Mats:

You can inspect the static CurrentPrincipal property of the Thread
class.
 
Yous hould be careful with this approach, because it is very possible to
change the principal for the thread, to something completely different from
a windows user.
 
Try this:
WindowsPrincipal threadPrincipal = Thread.CurrentPrincipal as
WindowsPrincipal;
if (threadPrincipal != null)
// Valid Windows principal
Console.WriteLine(threadPrincipal.Identity.Name);
else
// Current principal is not a Windows principal

Willy.
 
It's also possible to think you've changed the principal for a thread
without having done so (this doesn't do impersonation). The property
is for server applications to check, not really to modify.
 
Apologies, I thought I was in the asp.net group.

If this is a component used outside of ASP.NET use
WindowsIdentity.GetCurrent() from the System.Security.Principal
namespace.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top