Search code examples
asp.netpostbackhttpmodule

Reuse postback data in HttpModule


I have a custom HttpModule, which handles if a user need to pay a invoice. If the user has made a postback, but is "caught" in the invoice section of my HttpModule, I would like to repost the original postback, that the user made, after the invoice has been paid, so the user does not have to start over.

Example:

  1. The user fill out a form and submit it to the server
  2. The HttpModule identifies that the user has an unpaid invoice, and redirects the user to the payment page
  3. The user pays the bill
  4. The original post from point 1 is reposted and the user can continue

I've tried saving the HttpContext (from HttpContext.Current) in the session state and setting HttpContext.Current to the value in the session, when the bill has been paid, but it does not work.

Is it possible to reuse a postback after the HttpModule has interrupted the normal flow?

My HttpModule looks like this: class UnpaidInvoiceHttpModule : IHttpModule { private HttpApplication cHttpApp;

    public void Dispose(){}

    public void Init(HttpApplication context)
    {
        cHttpApp = context;
        context.PreRequestHandlerExecute += new EventHandler(CheckForUnpaidInvoices);
    }

    private void CheckForUnpaidInvoices(Object s, EventArgs e)
    {
        if (HttpContext.Current.Request.Path.EndsWith(".aspx") || HttpContext.Current.Request.Path.EndsWith(".asp") || HttpContext.Current.Request.Path == "/")
        {
            if (HttpContext.Current.Request.Path != "/login.aspx" 
                && HttpContext.Current.Request.Path != "/Payment/Default.aspx"
                && HttpContext.Current.Request.Path != "/Payment/Default_notice.aspx" 
                && HttpContext.Current.User != null)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    CustomUser mUser = ManagerSecurity.SecurityAPI.GetUser();      
                    if (mUser.HasUnpaidInvoices)
                    {
                        HttpContext.Current.Session["prepaymentHttpContext"] = HttpContext.Current;
                        HttpContext.Current.Response.Redirect("/Payment/Default.aspx");
                    }
                    else
                    {
                        if (HttpContext.Current.Session["prepaymentHttpContext"] != null)
                        {
                            HttpContext.Current = (HttpContext)HttpContext.Current.Session["prepaymentHttpContext"];
                        }
                    }
                }
            }
        }
    }
}

Solution

  • This link should provide you with everything you need to do what you describe. Note that this solution doesn't delay the post. It immediately reposts the data to a different page. You will have to modify it to store the name/value collection somewhere (perhaps in ViewState on the invoice page, or in a database) so it can be pulled up again after the invoice is paid. When the invoice is paid, you can pull up the name-value collection and pass it to the "redirect and post" method to put the user back on track to their original destination.