Hi all
I have an ASP.NET application that uses impersonate account to connect to
DB, this works good. But in some cases I need to put some methods in
ThreadPool. The methods from ThreadPool should do some foreground operations
with DB. But .NET throws me the sql exception "Login failed for user
'(null)'. Reason: Not associated with a trusted SQL Server connection.". The
thread from pool in execution proccess has an ASPNET account different from
my impersonated account. How can I resolve this problem, do you have any
suggestions?
with best regards
Viorel
Pavel Lebedinsky - 08 Feb 2005 04:25 GMT
Some thread pool implementations pass the impersonated token
to the thread pool thread, others do not. Based on the behavior
you're seeing it looks like the current .NET implementation
doesn't.
You can always do this yourself though. In the unmanaged world
you would do something like this:
1. hCurrentToken = OpenThreadToken
2. hDupToken = DuplicateToken(hCurrentToken)
3. Pass hDupToken as a parameter to your thread pool
work item
When your work item starts, it does this:
1. hThreadPoolToken = OpenThreadToken
2. SetThreadToken(hDupToken)
3. Perform the work
4. SetThreadToken(hThreadPoolToken)
5. CloseHandle(hDupToken)
You should be able to do something like this in .NET with
classes like WindowsIdentity. It even has a constructor that
takes a token handle as IntPtr. But I have no idea whether it
duplicates the token internally, whose responsibility it is to
close the handle etc. You might have to experiment a little
to find out how to use it properly.
> I have an ASP.NET application that uses impersonate account to connect to
> DB, this works good. But in some cases I need to put some methods in
[quoted text clipped - 7 lines]
> my impersonated account. How can I resolve this problem, do you have any
> suggestions?
Gaurav Khanna - 09 Feb 2005 07:22 GMT
Check this -
http://geekswithblogs.net/khanna/archive/2005/02/09/22441.aspx