Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Identity of Thread

In C#, how do I set the Identity of a Thread?

For example, if I have Thread MyThread, which is already started, can I change MyThread's Identity?

Or is this not possible?

like image 352
Duncan Avatar asked Nov 03 '08 14:11

Duncan


People also ask

How to identify a thread in Java?

Another way to uniquely identify a thread is to get thread's ID in Java. Thread class has getId () method which returns the thread’s ID. The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime. When a thread is terminated, this thread ID may be reused.

What is the current principal property in threading?

System. Threading Thread. Current Principal Property System. Threading Gets or sets the thread's current principal (for role-based security). public static System.Security.Principal.IPrincipal? CurrentPrincipal { get; set; } An IPrincipal value representing the security context. The caller does not have the permission required to set the principal.

What is the first thread in a process?

The first thread to be executed in a process is called the main thread. When a C# program starts execution, the main thread is automatically created. The threads created using the Thread class are called the child threads of the main thread. You can access a thread using the CurrentThread property of the Thread class.

What are the child threads of the main thread?

The threads created using the Thread class are called the child threads of the main thread. You can access a thread using the CurrentThread property of the Thread class.


1 Answers

You can set the Identity of a thread by creating a new Principal. You can use any Identity that inherits from System.Security.Principal.IIdentity, but you need a class that inherits from System.Security.Principal.IPrincipal that takes the type of Identity you are using.
For simplicity sake the .Net framework provides GenericPrincipal and GenericIdentity classes which can be used like this:

 using System.Security.Principal;   // ...  GenericIdentity identity = new GenericIdentity("M.Brown");  identity.IsAuthenticated = true;   // ...  System.Threading.Thread.CurrentPrincipal =     new GenericPrincipal(         identity,         new string[] { "Role1", "Role2" }     );   //...  if (!System.Threading.Thread.CurrentPrincipal.IsInRole("Role1"))  {       Console.WriteLine("Permission denied");       return;  } 

This won't however give you windows rights to stuff using the new identity. But it can be useful if you are developing a web site and want to create your own user management.

If you want to pretend to be a different Windows user than the account you are currently using then you need to use impersonation. An example of how to do this can be found in the Help for System.Security.Principal.WindowsIdentity.Impersonate(). There are limitations about which accounts the account you are running under can impersonate.

In some cases the .Net framework does impersonation for you. One example of where this occurs is if you are developing a ASP.Net web site and you have Integrated Windows Authentication switched on for the virtual directory or site you are running in.

like image 179
Martin Brown Avatar answered Oct 06 '22 01:10

Martin Brown