Redirect from a IHttpAsyncHandler

I've got the following class:

    public class FileAsyncHandler : IHttpAsyncHandler, IRequiresSessionState {

And I'm trying to redirect the user to a login page if they're not authenticated. I want to terminate execution and redirect. Sounds simple, right? Here's what I've tried so far inside a method called from BeginProcessRequest():

Response.Redirect("default.aspx", true);
// For some reason it steps right through this statement and continues down the page, even if I set endResponse to true.

Response.AddHeader("Location", "default.aspx");
Response.StatusCode = 301;
Response.End();
// Like Response.Redirect, this doesn't cause the page to stop executing.

Server.Transfer("default.aspx");
// This almost works, but gives me some strange errors on the page it tries to redirect. It's not a clean redirect.

Server.TransferRequest("default.aspx");
// Gives me an error about an integrated pipeline being required. I require a solution that will work within the ASP.NET properties as they are.


I'm guessing since it is IHttpAsyncHandler it is not the same as IHttpHandler and may require something unusual. I'm all out of ideas.
LVL 1
paulottAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David H.H.LeeCommented:
Hi paulott,
You can terminate the function instead, use "return" function.
eg:
protected void BeginProcessRequest(){
   Response.Redirect("default.aspx", true);
// For some reason it steps right through this statement and continues down the page, even if I set endResponse to true.

return;//<----Add this code

Response.AddHeader("Location", "default.aspx");
Response.StatusCode = 301;
Response.End();
// Like Response.Redirect, this doesn't cause the page to stop executing.

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.