Can't change response content in EndRequest event

Gabriel Luci

I'm attempting to turn a 401 response into a redirect without changing the response code. The code below was working before, but I did some fiddling to figure out some other issues, and now this won't work, and I can't figure out why.

What really boggles me is that when I step through the code, it hits the Response.Output.Write lines and does not throw any errors (I've also tried Response.Write), but when the browser receives the response, the normal IIS error page shows up.

Any idea why IIS would not be using the response content that I'm giving it?

This is IIS7 on Server 2008 R2. Website is running in .NET 4.0 (tried 4.5 and same thing happens).

This is the code in my Global.asax file:

    protected void Application_EndRequest(Object sender, EventArgs e) {
        if (Request.Url.ToString().Contains("WinLogin.aspx") && HttpContext.Current.Response.StatusCode == 401) {
            HttpContext.Current.Response.ClearContent();

            //If Windows authentication failed, redirect to the forms login page
            if (String.IsNullOrEmpty(Request.QueryString["ReturnUrl"])) {
                HttpContext.Current.Response.Output.Write(@"<html><head><script language='javascript'>self.location='/Login.aspx?ReturnUrl=/';</" + "script></head><body>401 Unauthorized</body></html>");
            } else {
                HttpContext.Current.Response.Output.Write(@"<html><head><script language='javascript'>self.location='/Login.aspx?ReturnUrl=" + Request.QueryString["ReturnUrl"] + "';</" + "script></head><body>401 Unauthorized</body></html>");
            }
        }
    }

Update: I figured out that this problem is only happening from remote machines (I was using a VM that's not joined to a domain to test). It works fine from my local machine. I'm going to do some searching online from that angle.

Gabriel Luci

Ok, I've figured it out. I have to enable detailed IIS errors in the web.config. The default is DetailedLocalOnly.

<httpErrors errorMode="Detailed" />

I had tried the customErrors tag, but that's only for ASP.NET errors, so it had no effect.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't change variable in Event

From Dev

Cookie changes made in EndRequest are not sent in Response

From Dev

Can't assign an http response content into an xml document on C#

From Dev

node js can't process sns event message content

From Dev

Bootstrap popover can't trigger event and content show incomplete

From Dev

Can't change IIS response code with URL Rewrite outbound rule

From Dev

How can I prevent multiple injection/execution of content script(s) in response to a single event?

From Dev

Event for change in text file content

From Dev

Can't stop Combobox_Change event VBA triggering another Combobox_Change event

From Dev

Can't focus on textarea after change event in Firefox

From Dev

Can't handle input "change" event or value after document load

From Dev

TinyMCE onPaste doesn't set content, paste with context menu doesn't trigger change event

From Dev

Web API 2 HTTP module EndRequest event being fired first?

From Dev

jQuery doesn't respond after change of div content - delegated event handler

From Dev

C# WebSocketSharp Event doesn't let me change WPF element content like Label text

From Dev

Why select option change event can't have this and event.target to get selected value?

From Dev

Can't make recursive call to Http.Request from within end event of Response

From Dev

Can't access event name from Facebook graph api response iOS

From Dev

Can't access event in

From Dev

Event can't be raised

From Dev

ckeditor content into textarea on change event - beginner

From Dev

Can I change URL of content?

From Dev

Plone/dexterity- In an event, how can I change the ownership role of a content type object that only an owner has view permission for?

From Dev

Can't get Ajax response

From Dev

Can't get response code

From Dev

Can't seem to get value from angular binding control in ng-change event

From Dev

AngularJS: Can't ignore change event on an input field after user cancels with confirmation dialog

From Dev

Why can't I trigger select change event using C# WebBrowser control?

From Dev

Can't make change image visibility ("hidden"/"visible") in the page with mouseover event listener in Javascript

Related Related

  1. 1

    Can't change variable in Event

  2. 2

    Cookie changes made in EndRequest are not sent in Response

  3. 3

    Can't assign an http response content into an xml document on C#

  4. 4

    node js can't process sns event message content

  5. 5

    Bootstrap popover can't trigger event and content show incomplete

  6. 6

    Can't change IIS response code with URL Rewrite outbound rule

  7. 7

    How can I prevent multiple injection/execution of content script(s) in response to a single event?

  8. 8

    Event for change in text file content

  9. 9

    Can't stop Combobox_Change event VBA triggering another Combobox_Change event

  10. 10

    Can't focus on textarea after change event in Firefox

  11. 11

    Can't handle input "change" event or value after document load

  12. 12

    TinyMCE onPaste doesn't set content, paste with context menu doesn't trigger change event

  13. 13

    Web API 2 HTTP module EndRequest event being fired first?

  14. 14

    jQuery doesn't respond after change of div content - delegated event handler

  15. 15

    C# WebSocketSharp Event doesn't let me change WPF element content like Label text

  16. 16

    Why select option change event can't have this and event.target to get selected value?

  17. 17

    Can't make recursive call to Http.Request from within end event of Response

  18. 18

    Can't access event name from Facebook graph api response iOS

  19. 19

    Can't access event in

  20. 20

    Event can't be raised

  21. 21

    ckeditor content into textarea on change event - beginner

  22. 22

    Can I change URL of content?

  23. 23

    Plone/dexterity- In an event, how can I change the ownership role of a content type object that only an owner has view permission for?

  24. 24

    Can't get Ajax response

  25. 25

    Can't get response code

  26. 26

    Can't seem to get value from angular binding control in ng-change event

  27. 27

    AngularJS: Can't ignore change event on an input field after user cancels with confirmation dialog

  28. 28

    Why can't I trigger select change event using C# WebBrowser control?

  29. 29

    Can't make change image visibility ("hidden"/"visible") in the page with mouseover event listener in Javascript

HotTag

Archive