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

LVL 3
ScottParkerAsked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
You can use impersonation on the current process to copy the file

http://weblogs.asp.net/ralfw/archive/2003/11/24/39479.aspx
0
 
lluddenCommented:
Can you just copy the pdf to a local temp file and display it from there?
0
 
ScottParkerAuthor Commented:
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.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
lluddenConnect With a Mentor Commented:
I have a similar situation.  What I did was to create a folder in System.IO.Path.GetTempPath for each user and clear it out each time the application ends.
0
 
ScottParkerAuthor Commented:
How would I copy the file.  The logged in user does not have security rights to the file.
0
 
ScottParkerAuthor Commented:
I tried that impersonate code already.  It does not seem to work with Windows 7.
0
 
CodeCruiserCommented:
It works for me. Show the code you are using to impersonate and to copy the file.
0
 
ScottParkerAuthor Commented:
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

0
 
CodeCruiserCommented:
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).
0
 
ScottParkerAuthor Commented:
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."}
0
 
CodeCruiserCommented:
Then it is a file security issue as the same code works for me in doing exactly the same thing.
0
 
ScottParkerAuthor Commented:
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?
0
 
ScottParkerAuthor Commented:
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.

0
 
CodeCruiserCommented:
>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.
0
 
ScottParkerAuthor Commented:
CodeCruiser,
Are you using Windows XP or Windows 7?
0
 
CodeCruiserCommented:
Both. Even 1 or 2 Vista
0
 
ScottParkerAuthor Commented:
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?
0
 
CodeCruiserCommented:
Looks like the impersonation code is not working as expected.

Try this class as well

http://vbnotebookfor.net/vbnb_impersonation.htm
0
 
ScottParkerAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.