473,464 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 4223
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.