I wrote a httphandler that intercepts all requests to PDF files that are in a particular location of my website. If they are available there, I would like the user to register himself in order to download the files.
If the user registration cookie is available, I would like to present the PDF to the user in the browser. But, instead of that, I just get a blank page. I already tried to use the Flush() method on the Response object but it did not produce any difference
in my results. Does anyone have a hint on how to solve this?
The problem is that the HttpHandler has no idea what it is supposed to do with the PDF. You must take care of the request yourself (after the if clause), and stream the file to the user, e.g using Response.TransmitFile
In fact the PDF is not local in the box where IIS is running. I am using a CMS solution and the file is located in the CMS server. The CMS itself has a handler that is supposed to do URL rewriting and forwarding the requests to the correct templates.
So, what I actually have is a request to a PDF file that is in a remote location. From within the http handler I cannot access it because I don't have the session object (thus, cannot load the file using the CMS API).
I just want the http handler to bypass the request if a particular cookie is available.
You should implement an IHttpHandlerFactory. If the the request is approved (has the defined cookie) return an instance of the CMS's IHttpHandler. Another option is to intercept the request in Global.asax instead.
...or you could implement it as an HttpModule instead.
i am trying to do something similar. when user requests a pdf file, i check if user has access to the file. if no access, redirect user to some other page. i am using HttpModule.
public void Init(HttpApplication app)
{
app.BeginRequest += BeginRequest;
}
public void Dispose()
{
}
private void BeginRequest(object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
//check user has access to requested file
//if no access, redirect to errorpage
}
my problem is when pdf file is requested, BeginRequest method isn't called when debugging in IIS settings. it is called if i debug in website setting. i have registered the httpmodule in web.config file and
and also mapped .pdf extension aspnet_isapi.dll. so in IIS settings, pdf file is displayed even user don't have access to it. what am i missing? this thing is driving me insane........... [:@]
I have a HttpHandler to handle the incoming request for .pdf files. The pdf file is generated at runtime using Crystal Reports.
I have added the handler in my web.config
<add verb="*" path="*.pdf" type="PdfHandler"/>
It runs perfectly on my local machine using the in-built server but when I publish it on IIS, the handler is not invoked. When I add the mapping in IIS for the file extension, it works properly.
Is there a way wherein I can achieve this without setting the file mapping in IIS? Can it be done just by editing the Web.config file?
i don't think you can achieve that without file mapping in IIS. there needs to be some way for IIS to know what actions to take if pdf file is requested.
how to secure files (pdf) in IIS 6.0 with asp.net 2.0, i used httphandelers, it works absolutely great.But now an new issue has been arrived.
What i did , i registerd ISAPI extension (.pdf) in IIS 6.0,to let process the request to ASP.Net runtime, there i used context.response.transmitfile to send the file to the browser, as client will be viewing file in browser not downloading, but in case when
file is bigger than 20 MB ,(file can be upto 100MB), it takes time, obviously it should take.
But when we redirect or request pdf file without registering ISAPI extension, browser starts showing file, means you see instantly first page or few pages while other are downloading, how to achive this kind of performance, and this kind of behaviour, with
letting asp.net runtime process the request.
I hope you guys get my question & if any explaination needed please ask.
here is code snipt for sending pdf files to client inside handeler
context.Response.Clear();
context.Response.AddHeader("content-disposition", "inline;filename=" + values[1] + ".pdf");
context.Response.ContentType = "application/pdf";
context.Response.BufferOutput = false;
context.Response.TransmitFile(physicalfilepath);
context.Response.Flush();
context.Response.Close();
context.Response.End();
Thanks in advance for sharing your wise suggestions with me.
gomesp
Member
9 Points
28 Posts
HttpHandler intercepting PDF requests
Aug 07, 2007 09:25 AM|LINK
Hi all,
I wrote a httphandler that intercepts all requests to PDF files that are in a particular location of my website. If they are available there, I would like the user to register himself in order to download the files.
For that I added the following handler:
<add path="/pdf/*" verb="*" type="RegisterUser" validate="true"/>
In my ProcessRequest method I have the following code:
public void ProcessRequest(HttpContext context)
{
string TargetUrl = "";
if (context.Request.Cookies.Get("USER_REGISTRATION") == null)
{
TargetUrl = string.Format("/?view=Register&DownloadLocation={0}", context.Request.Url.PathAndQuery);
context.Response.Redirect(TargetUrl);
}
}
If the user registration cookie is available, I would like to present the PDF to the user in the browser. But, instead of that, I just get a blank page. I already tried to use the Flush() method on the Response object but it did not produce any difference in my results. Does anyone have a hint on how to solve this?
Regards,
Pablo
gunteman
All-Star
22406 Points
3305 Posts
Re: HttpHandler intercepting PDF requests
Aug 07, 2007 11:33 AM|LINK
The problem is that the HttpHandler has no idea what it is supposed to do with the PDF. You must take care of the request yourself (after the if clause), and stream the file to the user, e.g using Response.TransmitFile
gomesp
Member
9 Points
28 Posts
Re: HttpHandler intercepting PDF requests
Aug 07, 2007 11:44 AM|LINK
In fact the PDF is not local in the box where IIS is running. I am using a CMS solution and the file is located in the CMS server. The CMS itself has a handler that is supposed to do URL rewriting and forwarding the requests to the correct templates.
So, what I actually have is a request to a PDF file that is in a remote location. From within the http handler I cannot access it because I don't have the session object (thus, cannot load the file using the CMS API).
I just want the http handler to bypass the request if a particular cookie is available.
gunteman
All-Star
22406 Points
3305 Posts
Re: HttpHandler intercepting PDF requests
Aug 07, 2007 11:54 AM|LINK
OK, I think I understand....
You should implement an IHttpHandlerFactory. If the the request is approved (has the defined cookie) return an instance of the CMS's IHttpHandler. Another option is to intercept the request in Global.asax instead.
...or you could implement it as an HttpModule instead.
gomesp
Member
9 Points
28 Posts
Re: HttpHandler intercepting PDF requests
Aug 07, 2007 12:42 PM|LINK
I think we are getting closer to a solution :-)
I will look into that and report back my findings.
gomesp
Member
9 Points
28 Posts
Re: HttpHandler intercepting PDF requests
Aug 10, 2007 12:16 PM|LINK
Hi, thanks for the help. Implementing the functionality as a HttpModule did the trick!
keeara
Participant
1092 Points
238 Posts
Re: HttpHandler intercepting PDF requests
Sep 03, 2007 08:25 AM|LINK
public void Init(HttpApplication app) { app.BeginRequest += BeginRequest; } public void Dispose() { } private void BeginRequest(object source, EventArgs e) { HttpApplication app = (HttpApplication)source; //check user has access to requested file //if no access, redirect to errorpage }my problem is when pdf file is requested, BeginRequest method isn't called when debugging in IIS settings. it is called if i debug in website setting. i have registered the httpmodule in web.config file and
and also mapped .pdf extension aspnet_isapi.dll. so in IIS settings, pdf file is displayed even user don't have access to it. what am i missing? this thing is driving me insane........... [:@]
any suggestions appreciated.
HttpModule
------------------
YogeshBagmar
Member
2 Points
1 Post
Re: HttpHandler intercepting PDF requests
Sep 11, 2007 05:08 AM|LINK
Hi,
I have a HttpHandler to handle the incoming request for .pdf files. The pdf file is generated at runtime using Crystal Reports.
I have added the handler in my web.config
<add verb="*" path="*.pdf" type="PdfHandler"/>
It runs perfectly on my local machine using the in-built server but when I publish it on IIS, the handler is not invoked. When I add the mapping in IIS for the file extension, it works properly.
Is there a way wherein I can achieve this without setting the file mapping in IIS? Can it be done just by editing the Web.config file?
Thanks in Advance,
Yogesh
keeara
Participant
1092 Points
238 Posts
Re: HttpHandler intercepting PDF requests
Sep 11, 2007 05:41 AM|LINK
i don't think you can achieve that without file mapping in IIS. there needs to be some way for IIS to know what actions to take if pdf file is requested.
------------------
adnan_raf
Member
2 Points
1 Post
Re: HttpHandler intercepting PDF requests
Sep 19, 2007 10:28 PM|LINK
how to secure files (pdf) in IIS 6.0 with asp.net 2.0, i used httphandelers, it works absolutely great.But now an new issue has been arrived.
What i did , i registerd ISAPI extension (.pdf) in IIS 6.0,to let process the request to ASP.Net runtime, there i used context.response.transmitfile to send the file to the browser, as client will be viewing file in browser not downloading, but in case when file is bigger than 20 MB ,(file can be upto 100MB), it takes time, obviously it should take.
But when we redirect or request pdf file without registering ISAPI extension, browser starts showing file, means you see instantly first page or few pages while other are downloading, how to achive this kind of performance, and this kind of behaviour, with letting asp.net runtime process the request.
I hope you guys get my question & if any explaination needed please ask.
here is code snipt for sending pdf files to client inside handeler
context.Response.Clear();
context.Response.AddHeader("content-disposition", "inline;filename=" + values[1] + ".pdf");
context.Response.ContentType = "application/pdf";
context.Response.BufferOutput = false;
context.Response.TransmitFile(physicalfilepath);
context.Response.Flush();
context.Response.Close();
context.Response.End();
Thanks in advance for sharing your wise suggestions with me.