Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I set the new thread principal?

4 views
Skip to first unread message

Andrea D'Onofrio [MSFT]

unread,
Apr 1, 2004, 10:44:12 AM4/1/04
to

You must declare the RevertToSelf function and the token variable as the class members:

[DllImport("advapi32")]

public static extern bool RevertToSelf();

protected IntPtr token;
You must save the token for the current thread and call the RevertToSelf before the new thread creation:
 
token = WindowsIdentity.GetCurrent().Token;

// This thread is currently impersonating so any thread we create won't have

// permission to impersonate. So we need to drop the current

// impersonation token so our new thread can impersonate.

RevertToSelf();

In the delegate function you must impersonate the identity:

// Impersonate using the Token property

WindowsIdentity wi =

new WindowsIdentity(token);

WindowsImpersonationContext ctx = wi.Impersonate();

HtH,
Andrea
 
--
This posting is provided "AS IS" with no warranties, and confers no rights.
 
 
> I start a new thread to run a long database query and show the progress bar:
>
> Thread DbThread = new Thread(new ThreadStart(dr.GetDataFromDb));
> DbThread.Start();
> Response.Redirect ("WaitPage.aspx");
>
> Problem is that the new thread ignores the identity of asp_netwp.exe worker process.
> There is  <identity impersonate="true" /> in the web.config and I use integrated SQL Security, that is the identity of the anonymous account in IIS metabase.  However, the new thread does not know anything about this and SQL connection fails.
>
> I can set processModel account, but maybe there is a better way?
>
> The Thread class has a static CurrentPrincipal member. I tried to use pass it inside the thread class, but it did not work...
>
> Thanks,
>
> -Stan
>

Andrea D'Onofrio [MSFT]

unread,
Apr 2, 2004, 8:32:43 AM4/2/04
to
If you need to create only one thread you don't need to use the
RevertToSelf, but if you must create other threads, if you don't call the
RevertToSelf, you 'll run into your original problem again.

HtH,
Andrea

--
This posting is provided "AS IS" with no warranties, and confers no rights.


"Stan" <nos...@yahoo.com> wrote in message

news:5E8DDD8C-4C00-4AE4...@microsoft.com...
> Actually, it works even withoug RevertToSelf. Here is the snippet:
>
> Main class:
> =======
> DbReader dr = new DbReader (WindowsIdentity.GetCurrent().Token);
> //RevertToSelf();


> Thread DbThread = new Thread(new ThreadStart(dr.GetDataFromDb));
> DbThread.Start();

> Response.Redirect ("Wait.aspx");
> =======
>
> Delegate:
> ======
> class DbReader
> {
> protected IntPtr token;
>
> public DbReader (IntPtr token)
> {
> this.token = token;
> }
>
> public void GetDataFromDb()
> {
> WindowsIdentity wi = new WindowsIdentity (token);
> WindowsImpersonationContext ctx = wi.Impersonate();
>
> // Get data...
> }
> }
>
> ==========
>
> Do I need RevertToSelf()?
>
> Thanks a lot for your post, it really helped
>
> -Stan


0 new messages