473,326 Members | 2,104 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,326 software developers and data experts.

Threading & Impersonation

I have an ASP.NET web service whose Web.Config is set to use impersonation

<authentication mode="Windows" />
<identity impersonate="true" />
Within a Web Method, I want to use Multi-threading to spawn off an
asynchronous process, as it takes quite long to return. How could I get the
worker thread to runas the same impersonated user on ASP.NET?

Dim worker As System.Threading.Thread = New
System.Threading.Thread(AddressOf reportManager.RunReport)
worker.Start()

Feb 16 '06 #1
1 1855
Welcome to the ASPNET newsgroup.

From your description, I understand you have an ASP.NET web application
which uses windows authentication & impersonation. And in your web pages,
you'll create new sub threads to do some tasks, however you're wondering
how to make those new created sub threads also running under the current
impersonated user in the main worker thread, correct?

Based on my understanding, for new created sub threads in ASP.NET web
application(e.g in web page), by default they will run under the ASP.NET
worker process's process identity(machine\ASPNET account for IIS5 and
Network Service account for IIS6). To change this behavior, we need to
explicitly do the impersonation in code within the new created sub thread's
code. We can first cached the current Context's WindowsIdentity in a shared
variable(page's member variable) and them use it to do the impersonation in
sub thread. Here is a msdn article discussing on impersonation in ASP.NET
2.0:

#How To: Use Impersonation and Delegation in ASP.NET 2.0
http://msdn.microsoft.com/library/en...3.asp?frame=tr
ue

You can have a look at the code of programmatic impersonation. In addition,
here is a simple page I used to test , this page programmatically
impersonate the current HttpContext's user and create a file.

=======================
public partial class _Default : System.Web.UI.Page
{
private WindowsIdentity _cachedId;

protected void Page_Load(object sender, EventArgs e)
{
_cachedId = Context.User.Identity as WindowsIdentity;

Thread thread = new Thread(new ThreadStart(ThreadProc));

thread.Start();

Thread.Sleep(3000);
CreateFileUnderCurrentUser();

}

protected void CreateFileUnderCurrentUser()
{
string fname = @"D:\users\stcheng\temp\imp{0}.txt";

FileStream fs = new FileStream(string.Format(fname,
DateTime.Now.Ticks), FileMode.Create);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);

sw.WriteLine("Hello txt file!");

sw.Close();

fs.Close();
}
protected void ThreadProc()
{
IntPtr token = IntPtr.Zero;
WindowsImpersonationContext impersonatedUser = null;

try
{
WindowsIdentity id = _cachedId;

impersonatedUser = id.Impersonate();

CreateFileUnderCurrentUser();

}
catch
{

}
finally
{
if (impersonatedUser != null)
impersonatedUser.Undo();

if (token != IntPtr.Zero)
CloseHandle(token);
}
}


#region ----PINVOKE FUNCTIONS----

[DllImport("advapi32.dll", SetLastError = true)]
static extern bool LogonUser(
string principal,
string authority,
string password,
LogonSessionType logonType,
LogonProvider logonProvider,
out IntPtr token);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr handle);
enum LogonSessionType : uint
{
Interactive = 2,
Network,
Batch,
Service,
NetworkCleartext = 8,
NewCredentials
}
enum LogonProvider : uint
{
Default = 0, // default for platform (use this!)
WinNT35, // sends smoke signals to authority
WinNT40, // uses NTLM
WinNT50 // negotiates Kerb or NTLM
}
#endregion
}

==========================

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Feb 17 '06 #2

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

Similar topics

1
by: Jerry Negrelli | last post by:
I'm running an ASP.NET application which uses impersonation & Windows authentication to set the current Principal. One of my methods was taking an awful long time to run, so I decided to launch...
8
by: Z D | last post by:
Hello, I'm having a strange problem that is probably due to my lack of understanding of how threading & COM Interop works in a WinForms.NET application. Here's the situation: I have a 3rd...
0
by: matthew | last post by:
I am trying to create an Outlook application object in my asp.net page, but i get a message saying it could not create instance of server. The Event log tells me that 'The server {xxx-xxx-xxx} did...
4
by: TipTop | last post by:
I am trying to use integrated Windows authentication and impersonation to run a page that accesses SQL Server via integrated security. It's not working -- sort of. I've set IIS security to...
2
by: Matthew Lee | last post by:
I am using a C# / ASP.NET application to fire an XCOPY job from the webserver to a UNC share. This setup works fine when copying to a local machine but refuses to work on the remote copy. The...
4
by: Makarand Keer | last post by:
Hi All I have problem in using Threading. I have ASP.NET application in which I am using multithreading to start a process. Now the object instances which are used in this thread access...
5
by: Adrian Bezzina | last post by:
Hi Everyone, I am having difficulty in starting a new thread in ASP.NET while processing a page request. I have an aspx page that runs a whole lot of batch reports. These batch reports could...
2
by: Olivier Matrot | last post by:
I need to impersonate in ASP.NET in order to print a document to a printer. The guidelines in http://support.microsoft.com/kb/306158/EN-US/ do not tell about loading the user profile...
0
ammoos
by: ammoos | last post by:
hi friends pls help me.. i got an assignment which i feel very difficult to me.. i dont have more knowledge about multi-threading in .net... the assignment details is below.... pls help me... i...
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
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.