Running spawned worker thread as impersonated user

G

Guest

I have an ASP.NET web service whose Web.Config is set to use impersonation

<authentication mode="Windows" />
<identity impersonate="true" />


Within a Web Method, I want to use Multi-threading to spawn off an
asynchronous process, as it takes quite long to return. How could I get the
worker thread to runas the same impersonated user on ASP.NET?

Dim worker As System.Threading.Thread = New
System.Threading.Thread(AddressOf reportManager.RunReport)
worker.Start()
 
B

Bruce Barker

you need to pass the creditials of the current thread to the new created
thread.


class foo()
{
private WindowsIdentity mThreadIdentity = null;
[DllImport("advapi32")] static extern bool RevertToSelf();

public void StartThread()
{
// setup thread

ThreadStart ts = new ThreadStart(RunReport);
Thread t = new Thread(ts);
mThreadIdentity = WindowsIdentity.GetCurrent();

// drop any impersonation

RevertToSelf();

// start thread

t.Start();

// restore thread identity

mThreadIdentity.Impersonate();

}

private void RunReport()
{
mThreadIdentity.Impersonate();
// do whatever
}
}
 
G

Guest

my reportManager.RunReport is in a *different* class from the web method that
spawn it off. Hence, it won't be able to reference mThreadIdentity.

In addition, could you clarify the definition of [DllImport("advapi32")]
static extern bool RevertToSelf(); .e.g. what is the EntryPoint?




Bruce Barker said:
you need to pass the creditials of the current thread to the new created
thread.


class foo()
{
private WindowsIdentity mThreadIdentity = null;
[DllImport("advapi32")] static extern bool RevertToSelf();

public void StartThread()
{
// setup thread

ThreadStart ts = new ThreadStart(RunReport);
Thread t = new Thread(ts);
mThreadIdentity = WindowsIdentity.GetCurrent();

// drop any impersonation

RevertToSelf();

// start thread

t.Start();

// restore thread identity

mThreadIdentity.Impersonate();

}

private void RunReport()
{
mThreadIdentity.Impersonate();
// do whatever
}
}


Patrick said:
I have an ASP.NET web service whose Web.Config is set to use impersonation

<authentication mode="Windows" />
<identity impersonate="true" />


Within a Web Method, I want to use Multi-threading to spawn off an
asynchronous process, as it takes quite long to return. How could I get
the
worker thread to runas the same impersonated user on ASP.NET?

Dim worker As System.Threading.Thread = New
System.Threading.Thread(AddressOf reportManager.RunReport)
worker.Start()
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top