Search code examples
asp.netsessionforms-authenticationabcpdf

Pass Authenticated Session In Another Request in ASP.NET


I am using ABCPDF.net to render my HTML to PDF pages, but I want to protect these using the current authenticated session. However, the request to get the HTML to render the PDF doesnt carry over the session, but creates a new one. I am using ASP.NET Membership, and passwigin the session ID and creating a cookie on the ASP.NET request.

    var sessionId = HttpUtility.ParseQueryString(Url)["sid"];

    if(sessionId != null)
    {
        doc.HtmlOptions.HttpAdditionalHeaders = string.Format("Cookie: 
                                    ASP.NET_SessionId={0}", sessionId);
    }

I read this in ABCPDF.net documentation and am doing just that, but the request always uses a different session.

httpadditionalheaders

Do I need to pass something else, or can I do something else?


Solution

  • Fixed it by sending the auth cookie in the URL, and updating the cookie.

    Url To Call

    Url.ActionAbsolute(MVC.Events.Pools(eventId).AddRouteValue(FormsAuthentication.FormsCookieName, Request.Cookies[FormsAuthentication.FormsCookieName].Value)
    

    Global.asax

      protected void Application_BeginRequest()
            {
                try
                {
                    string auth_cookie_name = FormsAuthentication.FormsCookieName;
    
                    if (HttpContext.Current.Request.Form[FormsAuthentication.FormsCookieName] != null)
                    {
                        UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[FormsAuthentication.FormsCookieName]);
                    }
                    else if (HttpContext.Current.Request.QueryString[FormsAuthentication.FormsCookieName] != null)
                    {
                        UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[FormsAuthentication.FormsCookieName]);
                    }
    
                }
                catch
                {
                }
            }
    
            void UpdateCookie(string cookieName, string cookieValue)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookieName);
                if (cookie == null)
                {
                    cookie = new HttpCookie(cookieName);
                    HttpContext.Current.Request.Cookies.Add(cookie);
                }
                cookie.Value = cookieValue;
                HttpContext.Current.Request.Cookies.Set(cookie);
    
        }