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

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top