?
Solved

Impersonate User to open Adobe PDF file

Posted on 2011-05-04
19
Medium Priority
?
971 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:ScottParker
  • 10
  • 7
  • 2
19 Comments
 
LVL 18

Expert Comment

by:lludden
ID: 35694110
Can you just copy the pdf to a local temp file and display it from there?
0
 
LVL 3

Author Comment

by:ScottParker
ID: 35697375
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
 
LVL 18

Assisted Solution

by:lludden
lludden earned 1000 total points
ID: 35698535
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:ScottParker
ID: 35699915
How would I copy the file.  The logged in user does not have security rights to the file.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1000 total points
ID: 35701965
You can use impersonation on the current process to copy the file

http://weblogs.asp.net/ralfw/archive/2003/11/24/39479.aspx
0
 
LVL 3

Author Comment

by:ScottParker
ID: 35705428
I tried that impersonate code already.  It does not seem to work with Windows 7.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35705853
It works for me. Show the code you are using to impersonate and to copy the file.
0
 
LVL 3

Author Comment

by:ScottParker
ID: 35706619
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35706648
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
 
LVL 3

Author Comment

by:ScottParker
ID: 35706694
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35706886
Then it is a file security issue as the same code works for me in doing exactly the same thing.
0
 
LVL 3

Author Comment

by:ScottParker
ID: 35707671
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
 
LVL 3

Author Comment

by:ScottParker
ID: 35707737
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35716210
>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
 
LVL 3

Author Comment

by:ScottParker
ID: 35719814
CodeCruiser,
Are you using Windows XP or Windows 7?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35720075
Both. Even 1 or 2 Vista
0
 
LVL 3

Author Comment

by:ScottParker
ID: 35740976
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35745024
Looks like the impersonation code is not working as expected.

Try this class as well

http://vbnotebookfor.net/vbnb_impersonation.htm
0
 
LVL 3

Author Closing Comment

by:ScottParker
ID: 35769063
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update 21-May-2015: I temporarily removed the source code and the code snippets to make major changes to the program. Regards, Joe A recent question here at Experts Exchange piqued my interest, so I decided to provide a thorough solution and publ…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this second video of the Xpdf series, we discuss and demonstrate the PDFimages utility, which, in a single command, is able to extract all the images from a PDF file and save each one in a separate image file (PBM, PPM, or JPG). Download and inst…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question