Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dispaly pdf through impersonate does not work

Status
Not open for further replies.

GPhilipp

Programmer
Apr 12, 2010
20
US
Hello Everybody.

I got some problems with impersonation.

I have created a asp.net intranet site. The short of it is that the end user gets a grid displayed with pdf files. The pdf files are located on another sever behind locked security to which the enduser has no directly access.

When the user clicks on a file in the grid the site should display the pdf file in an embeded pdf viewer on that page. In order to accomplish this i use impersonation in the code-behind secion.
Using impersonationContext As New WindowsImpersonationContextFacade()
Me.ShowPdf1.FilePath = c_TempFilePath
End Using
(Note: this code is used from with some modifications to fit my purposes. I also know this works because i use the exact same code in a vb.net app to access the same files successfully.)

As long as i am testing this (domain admin with rights to that repository) it works fine - so i know the basic loading of the file works. However, for the enduser it does not work. They don't get an error, but the pdf does not display.

My assumption is that the file does not actually get loaded in the behind-code but that code only passes the info to the client and the client loads the file.

If that is true, that would explain the failure as the client does not have the read right to the repository and the impersonation is already gone.

Question:
- am i correct with my assumption?
- how do I do this correctly?

As always - thank you for your great help!
 
Code:
Me.ShowPdf1.FilePath = c_TempFilePath
does this only set the file path, or does it read the file?
if this is only setting the file path, then you do not need impersonation at this point. Impersonation is only required when you access the file.

this is probably why the code works for you, but not the end user.

another option, slightly more complex. is to setup a protocol between the web server and the file server. have the web server request the file from the file server. the file server will send a byte[] of the file back to the web server. the web server can then load the bytes for the user to see the pdf.

This way you do not need to mess with impersonation code. using a framework like agatha you should be able to easily get this up and running.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Hello Jason

Thanks for your reply.

Me.ShowPdf1 is an object which receives the file path and name. It is based on this article ( to display pdf files.

I was also thinking about streaming, loading the file into memory and then passing it to the pdf viewer, but i have no idea how to go about this.

Also, am I correct in assuming that in the behind-code it does not realy load the file into the reader/viewer but only tells the client how to get it?

Thanks
 
the pdf isn't loaded until the iframe is rendered. this means a request to the file server isn't made until the client processes the iframe. having impersonation to set a string property does nothing.

1. get rid of the PdfViewer control. this is just bloat. instead create a link that points to our file proxy (step 2).
HTML:
<a href="fileproxy.ashx?file=encoded_path_to_file.pdf">View PDF</a>
2. add an generic handler (ashx) to your project. I named it fileproxy
3. using impersonation, write the file to the response stream.
Code:
public class FileProxy : IHttpHandler
{
   public void ProcessRequest(HttpContext context)
   {
       var filename = HttpUtil.Decode(context.Request.Params["file"]);
       using (var impersonation  new WindowsImpersonationContextFacade())
       {
           context.Response.WriteFile(filename);
       }
   }

   public bool IsReusable {get{return true;}}
}

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Hello Jason

I'll give that a try and let you know the result.

Thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top