473,406 Members | 2,404 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,406 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 4222
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.