473,499 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ 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 4225
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
1795
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
1751
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
2186
by: hazz | last post by:
is that possible from a C++ app? thank you. -Greg
0
995
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
391
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
1961
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
850
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
12168
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
5728
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
1895
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
7132
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
7009
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
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
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
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.