在ASP.NET的另一个请求中传递经过身份验证的会话

时间:2012-10-05 17:37:18

标签: asp.net session forms-authentication abcpdf

我正在使用ABCPDF.net将我的HTML呈现为PDF页面,但我想使用当前经过身份验证的会话来保护这些页面。但是,获取HTML以呈现PDF的请求不会延续会话,而是创建一个新会话。我正在使用ASP.NET Membership,并传递会话ID并在ASP.NET请求上创建cookie。

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

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

我在ABCPDF.net文档中读到了这个,并且我正在这样做,但请求总是使用不同的会话。

httpadditionalheaders

我是否需要传递其他内容,或者我可以做其他事情吗?

1 个答案:

答案 0 :(得分:1)

通过在URL中发送auth cookie并更新cookie来修复它。

要致电的网址

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);

    }