473,320 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I set the new thread principal?

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

-Sta

Nov 18 '05 #1
3 4219
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.
"Stan" <no****@yahoo.com> wrote in message news:C0**********************************@microsof t.com...
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

Nov 18 '05 #2
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 DbReade

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 helpe

-Stan
Nov 18 '05 #3
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" <no****@yahoo.com> wrote in message
news:5E**********************************@microsof t.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

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mats-Lennart Hansson | last post by:
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...
9
by: Víctor | last post by:
Hello, I'm filling a array of System.Diagnostic.Process by using GetProcesses() method. Due to a retard on this method, I do the call using a function and it passing like a delegate to one...
11
by: hazz | last post by:
is that possible from a C++ app? thank you. -Greg
0
by: Jon Paugh | last post by:
What is the advantage of using Thread.CurrentPrincipal to store a custom principal vs storing the custom principal object in the user's session?
2
by: Stan | last post by:
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")...
2
by: C | last post by:
All, I am trying to run my BO in a separate thread, so that I can give the user a visual of the status of my process in a asp.net page/site. All works fine, IF the ASPNET user has admin rights,...
0
by: Mythran | last post by:
Problem #1: We have an server application in COM+ on the same machine we have a web application on. The web application creates a Principal and Identity object containing the users information...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
5
by: Brian | last post by:
I have an file based asp.net application the creates a thread to do some background printing. It works fine but when the application is deployed on a web server, the following error occurs in the...
2
by: phil | last post by:
My ASP.NET application sets up impersonate in the config file: <identity impersonate="true" userName="m1\aspuser" password="xyz" / Later, within my application, I create a thread: x= New...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.