Home All Groups Group Topic Archive Search About

Act as user a different user

Author
1 Apr 2005 7:43 PM
Scott
How do I get a windows application to run as a different user.  I am
user A and I want to act as user B.  I need to copy files out of a
shared directory on a server that user B will only have access to.  In
the function CopyFile it appears thats everything is OK it has the User
B identity object but the Server does not reconize the requerst and
denies access to the file.  Is there a way to get the OS to reconize
the change and not just the application?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Dim wp As System.Security.Principal.WindowsPrincipal
        Dim Domain As String = "ITFirst-HQ"
        Dim sPassword As String = "password"
        Dim sUser As String = "vsecure"
        Dim wi As WindowsImpersonationContext
        wi = NetworkSecurity.ImpersonateUser(Domain, sUser, sPassword,
LogonType.LOGON32_LOGON_NETWORK,
LogonProvider.LOGON32_PROVIDER_DEFAULT, True)
        wp = New WindowsPrincipal(NetworkSecurity.getIdentity(Domain,
sUser, sPassword, LogonType.LOGON32_LOGON_NETWORK,
LogonProvider.LOGON32_PROVIDER_DEFAULT, True))
        AppDomain.CurrentDomain.SetThreadPrincipal(wp)

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
        Dim th As Threading.Thread = New Threading.Thread(AddressOf
CopyFile)
        th.Start()
    End Sub
    Private Sub CopyFile()
        Dim th As Threading.Thread =
System.Threading.Thread.CurrentThread
        Dim wp As WindowsPrincipal = th.CurrentPrincipal
        Dim id As WindowsIdentity = wp.Identity
        System.IO.File.Copy("\\server\SecureTest\test.pdf",
"C:\test.pdf", True)

    End Sub

Author
1 Apr 2005 8:05 PM
Joe Kaplan (MVP - ADSI)
Are you sure your new thread has the right impersonation token on it?  In
..NET 1.1, new threads don't automatically inherit the impersonation
information.  You might also need to use LOGON32_LOGON_NETWORK_CLEARTEXT,
but I'm not sure about that.

Joe K.

Show quoteHide quote
"Scott" <scott.remi***@itfirst.com> wrote in message
news:1112384593.539101.155450@z14g2000cwz.googlegroups.com...
> How do I get a windows application to run as a different user.  I am
> user A and I want to act as user B.  I need to copy files out of a
> shared directory on a server that user B will only have access to.  In
> the function CopyFile it appears thats everything is OK it has the User
> B identity object but the Server does not reconize the requerst and
> denies access to the file.  Is there a way to get the OS to reconize
> the change and not just the application?
>
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>        Dim wp As System.Security.Principal.WindowsPrincipal
>        Dim Domain As String = "ITFirst-HQ"
>        Dim sPassword As String = "password"
>        Dim sUser As String = "vsecure"
>        Dim wi As WindowsImpersonationContext
>        wi = NetworkSecurity.ImpersonateUser(Domain, sUser, sPassword,
> LogonType.LOGON32_LOGON_NETWORK,
> LogonProvider.LOGON32_PROVIDER_DEFAULT, True)
>        wp = New WindowsPrincipal(NetworkSecurity.getIdentity(Domain,
> sUser, sPassword, LogonType.LOGON32_LOGON_NETWORK,
> LogonProvider.LOGON32_PROVIDER_DEFAULT, True))
>        AppDomain.CurrentDomain.SetThreadPrincipal(wp)
>
> AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
>        Dim th As Threading.Thread = New Threading.Thread(AddressOf
> CopyFile)
>        th.Start()
>    End Sub
>    Private Sub CopyFile()
>        Dim th As Threading.Thread =
> System.Threading.Thread.CurrentThread
>        Dim wp As WindowsPrincipal = th.CurrentPrincipal
>        Dim id As WindowsIdentity = wp.Identity
>        System.IO.File.Copy("\\server\SecureTest\test.pdf",
> "C:\test.pdf", True)
>
>    End Sub
>