Identity Impersonate - Multithreading?

R

rowe_newsgroups

Forgive me if I'm being ignorant, but is the identity tab in the
web.config ignored by background threads?

I have written a class that has both a synchronous and asynchronous
mode for querying an Access database located on a network share. To
get access to the network share I am using the identity tag with
impersonate="true" and a valid user name and password. The odd thing
is that the synchronous method runs fine, but when multithreaded, I
get an OleDb exception saying the the Jet OleDb provider could not
find the path specified in the connection string - it's as if the
identity tag is being ignored!?

Thanks in advance for any input!

Thanks,

Seth Rowe
 
B

bruce barker

when the NT creates a thread, the identity is the process identity, not
the creating threads identity.

to get around this, you can pass the credentials to the new thread. you
will need to use the win api -- see DuplicateToken. you may also need a
RevertToSelf. you then pass the token to to new WindowsIdentity, and
call impersonate method.

google these terms to get samples.

-- bruce (sqlwork.com)
 
R

rowe_newsgroups

IIRC, The background thread executes under the Application Pool identity.
Make sure you pass the correct identity to the Jet OleDb provider.

Review this article/download the code and see if your code
conforms to what Joshua says works for him :

http://flimflan.com/blog/SafelyRunningBackgroundThreadsInASPNET20.aspx

Oh how I wish I would have posted this yesterday!

I'll give your link a read as well as bruce's suggestions and let you
know how it turns out.

Thanks to both of you!

Thanks,

Seth Rowe
 
R

rowe_newsgroups

Oh how I wish I would have posted this yesterday!

I'll give your link a read as well as bruce's suggestions and let you
know how it turns out.

Thanks to both of you!

Thanks,

Seth Rowe

Okay, in the end I didn't use either of your suggestions, but crafted
my own based on some API documentation I read through.

Below is my solution if any future archive browsers need it:

//////////////////
Imports System.Security.Principal
Imports System.Runtime.InteropServices

Public Class MyClassToBeThreaded

<DllImport("advapi32.dll")> _
Protected Shared Function LogonUser(ByVal lpszUserName As String,
_
ByVal lpszDomain As String, _
ByVal lpszPassword As String, _
ByVal dwLogonType As Integer, _
ByVal dwLogonProvider As Integer,
_
ByRef phToken As IntPtr) As
Boolean
End Function

'// This is the thread to be called asynchronously,
'// so do the impersonation calls here
Public Sub DoTheWork()
Try
Dim hToken As IntPtr
If LogonUser("username", "domain", "password", 2, 0,
hToken) Then
Using wi As WindowsIdentity = New
WindowsIdentity(hToken), wic As WindowsImpersonationContext =
wi.Impersonate()
'// Do the actually work here

End Using
End If
Catch
'// Handle the exception if the impersonation
'// blows up in your face :)
End Try
End Sub

End Class
/////////////////////

Thanks,

Seth Rowe
 
J

Juan T. Llibre

re:
!> Okay, in the end I didn't use either of your suggestions, but crafted
!> my own based on some API documentation I read through.

Fantastic, Seth!

Thanks for sharing the code.
It should help someone down the line...





Oh how I wish I would have posted this yesterday!

I'll give your link a read as well as bruce's suggestions and let you
know how it turns out.

Thanks to both of you!

Thanks,

Seth Rowe

Okay, in the end I didn't use either of your suggestions, but crafted
my own based on some API documentation I read through.

Below is my solution if any future archive browsers need it:

//////////////////
Imports System.Security.Principal
Imports System.Runtime.InteropServices

Public Class MyClassToBeThreaded

<DllImport("advapi32.dll")> _
Protected Shared Function LogonUser(ByVal lpszUserName As String,
_
ByVal lpszDomain As String, _
ByVal lpszPassword As String, _
ByVal dwLogonType As Integer, _
ByVal dwLogonProvider As Integer,
_
ByRef phToken As IntPtr) As
Boolean
End Function

'// This is the thread to be called asynchronously,
'// so do the impersonation calls here
Public Sub DoTheWork()
Try
Dim hToken As IntPtr
If LogonUser("username", "domain", "password", 2, 0,
hToken) Then
Using wi As WindowsIdentity = New
WindowsIdentity(hToken), wic As WindowsImpersonationContext =
wi.Impersonate()
'// Do the actually work here

End Using
End If
Catch
'// Handle the exception if the impersonation
'// blows up in your face :)
End Try
End Sub

End Class
/////////////////////

Thanks,

Seth Rowe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top