I'm about to throw myself off a building over this.
I support a few hundred users of a particular application that is updated frequently - said updates involving nothing more than replacing the main .exe with a new version. I've written a dashboard for myself that allows me to stage the new version in advance, push it out on the night of the upgrade, rollback, etc. But I've always wanted to write a simple wrapper that sits on the users' desktops and allows them to swap out the .exe on their own.
Problem is that company policy forbids the users from having write rights to the application directory.
So I've tried various types and forms of impersonation in VB - console and form applications - and, though I think I've done everything exactly as instructed in various blogs and by Microsoft, and although stepping through the programs indicates that I have indeed impersonated a user with admin rights on all of the machines, when a regular user runs the code, it fails to copy the new file, throwing the error that "the user doesn't have rights to the directory."
The heart of the code, which anyone who's ever tried to do this will probably recognize, looks like this:
Using safeTokenHandle
Dim success As String
If returnValue Then success = "Yes" Else success = "No"
TextBox1.Text += "Did LogonUser succeed? " & success & vbCrLf
TextBox1.Text += "Value of Windows NT token: " & safeTokenHandle.DangerousGetHandle().ToString() & vbCrLf
TextBox1.Text = "Before impersonation: " & WindowsIdentity.GetCurrent().Name & vbCrLf
Using newId As New WindowsIdentity(safeTokenHandle.DangerousGetHandle())
Using impersonatedUser As WindowsImpersonationContext = newId.Impersonate()
TextBox1.Text += "After impersonation: " & WindowsIdentity.GetCurrent().Name & vbCrLf
System.IO.File.Copy("\\MyServer\SourceFiles\TheProgram.exe", "c:\Program Files\AppDirectory\TheProgram.exe", True)
End Using
End Using
End Using
The text box updates do indeed show that the file.copy code is being run under the newID, but the copy still fails with a rights issue.
What the heck am I missing?