+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / CustomPdfHandler not working with search result widget

CustomPdfHandler not working with search result widget

2 posts, 0 answered
  1. Nirav
    Nirav avatar
    8 posts
    Registered:
    24 Jun 2013
    25 Jun 2013
    Link to this post
    Hello Team,

    I just created CustomPdfHandler to restrict users to see pdf of my application. It works fine with content block widget means when i give link of pdf in content block widget it is not allowing all users to see pdf but when i search pdf from search widget and click on search result links, handler is not working and all users are able to see pdf files.!! Can any one help please ?

    Regards.
  2. Nirav
    Nirav avatar
    8 posts
    Registered:
    24 Jun 2013
    25 Jun 2013 in reply to Nirav
    Link to this post
    My custom handler is in App-Code/CustomPdfHandler.cs file something like this:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;


    //namespace Login_role.App_Code
    //{
    public class CustomPdfHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        /// <summary>
        /// You will need to configure this handler in the web.config file of your 
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        public void ProcessRequest(HttpContext context)
        {
            if (context.Session["UserName"] == null)

            //if (!context.User.Identity.IsAuthenticated)
            {
                context.Response.Redirect("/Login Role/Login.aspx?ReturnUrl=" + HttpContext.Current.Request.RawUrl.ToString());
                context.Response.StatusCode = 401;
                return;
            }

            var url = context.Request.CurrentExecutionFilePath;

            if (string.IsNullOrEmpty(url)) return;

            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("filename={0}", url));
            HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
            HttpContext.Current.Response.WriteFile(url);
            HttpContext.Current.Response.End();
        }

        public bool IsReusable
        {
            get { return false; }
        }
    }
    //}

    And In web.config file/<httpHandlers>

    <add verb="*" path="*.pdf" type="CustomPdfHandler, App_Code" validate="false" />

    and

    In <handlers>

    <add name="PdfRestrict" path="*.pdf" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="Script" preCondition="classicMode,runtimeVersionv4.0,bitness32" />

    I did above codes to handle Custom handlers.
2 posts, 0 answered