Link to home
Start Free TrialLog in
Avatar of ScottParker
ScottParkerFlag for United States of America

asked on

Impersonate User to open Adobe PDF file

We have several thousand PDF files on a network folder   //myserver/pdffiles/
I have a VB.NET application that has a button on it that the user will click and it opens up one of the PDF files.

Users do not have security over the folder or pdf files.  
My network people created me a username which does have access to all of the files, I thought I could impersonate that user in the code to open the file.  I get an error from adobe when I do this.  “An internal error occurred.”

I have tested the username/login peice by using a different password and I end up with an error message that tells me its wrong.
I have also tested opening a local copy of a pdf file without the username/password in the process and it opens fine.
I am currently on Windows 7 and coding with Visual Studio 2010.
This application will primarly run on Windows XP Pro (only a couple Win 7) machines on our internal network.


Here is the code I am using.
 
Dim readerPath As String = Registry.GetValue( _
                "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe" & "AcroRd32.exe", _
                "Path", "Key does not exist")

            Dim strDrawingName As String

            strDrawingName = "\\myserver\pdffiles\Cost_Test.pdf"
            Dim startInfo As New ProcessStartInfo

            startInfo.FileName = readerPath
            startInfo.Arguments = strDrawingName
            startInfo.UserName() = "PDFAdmin"

            startInfo.Password = New System.Security.SecureString()
            For Each c As Char In "adminpassword"
                startInfo.Password.AppendChar(c)
            Next
            startInfo.UseShellExecute = False
            Process.Start(startInfo)

Open in new window

Avatar of lludden
lludden
Flag of United States of America image

Can you just copy the pdf to a local temp file and display it from there?
Avatar of ScottParker

ASKER

If that is what I have to do, then I guess I could.   But I do not like the idea of the orphaned files on their local machines.
I was really hoping to be able to find a cleaner solution.

I was under the impression that this is what the "Process.Start" function was made for.
SOLUTION
Avatar of lludden
lludden
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How would I copy the file.  The logged in user does not have security rights to the file.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried that impersonate code already.  It does not seem to work with Windows 7.
It works for me. Show the code you are using to impersonate and to copy the file.
here is the code I am using.
Dim aa As New AliasAccount("AdminUser", "AdminPassword", "Domain")

            Dim strNewPath As String = System.IO.Path.GetTempPath
            Dim strNewFile As String = strNewPath & "Cost_Test.pdf"
            Dim strDrawingName As String
            strDrawingName = "\\servername\test\Cost_Test.pdf"

            aa.BeginImpersonation()
            If System.IO.File.Exists(strNewFile) Then
                System.IO.File.Delete(strNewFile)
            End If
            System.IO.File.Copy(strDrawingName, strNewFile)

            aa.EndImpersonation()

            Dim readerPath As String = Registry.GetValue( _
               "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe", _
                "Path", "Key does not exist")
            readerPath = readerPath & "AcroRd32.exe"

            Dim startInfo As New ProcessStartInfo

            startInfo.FileName = readerPath
            startInfo.Arguments = strNewPath

Open in new window

What is strNewFile? Is it within current user's folder (temp folder)? Just remember that when you impersonate as a different user, you would be unable to access files/folders inside the current user's folder (such as desktop).
I changed the strNewFile to just be c:\drawingtemp\
I am still getting the following error message..
{"Access to the path '\\servername\test\Cost_Test.pdf' is denied."}
Then it is a file security issue as the same code works for me in doing exactly the same thing.
The logged in user does not have access to the network file.  That is the whole point in doing the impersonation.
Are you sure your tests have been the same?   your not a network admin or some such thing that has rights to the file?

In windows 7, if I right click on the exe of the program and click Run as Administrator, then the file is copied.
There must be some difference in your user setup then my test one?
I have also figured out that if I run Visual Studio as Administrator, it also works in "Debug" mode.
But if I do not, then it seems that the Impersonate code does not work.

>The logged in user does not have access to the network file.  That is the whole point in doing the impersonation

I was talking about the impersonated user.


>your not a network admin or some such thing that has rights to the file?

More than 20 users are using the app and they are all restricted users.
CodeCruiser,
Are you using Windows XP or Windows 7?
Both. Even 1 or 2 Vista
Seems rather strange but on the Windows XP machine, the impersonate code is causeing the user to not be able to access Web sites outside of our local network. (i.e. www.google.com)
Even though the "EndImpersonation()" function is called, when a user tries to open their browser and go to google, its like they still have the security of the impresonated user. (who does not have internet access)

Any thoughts on this?
Looks like the impersonation code is not working as expected.

Try this class as well

http://vbnotebookfor.net/vbnb_impersonation.htm
I dont like the solution I ended up with but it will have to do for now.

I ended up using the impersonate code to copy the file localy, then display the local file in Adobe.
The impersonate code needs some work due to the internet issues mentioned earlier.  
I just wanted to get the question closed because I wont be able to work on this again for a couple of weeks.