A preferred way to check if asp.net web application is in debug mode during runtime?

后端 未结 3 641
陌清茗
陌清茗 2021-02-02 06:11

During compile time I can do a check like

#if DEBUG
    Log(\"something\");
#endif

But what would be the preferred to check if debug=\"f

相关标签:
3条回答
  • 2021-02-02 06:56

    HttpContext.IsDebuggingEnabled

    https://docs.microsoft.com/en-us/dotnet/api/system.web.httpcontext.isdebuggingenabled

    0 讨论(0)
  • 2021-02-02 07:05

    In some cases, you may need HttpContext.Current.IsDebuggingEnabled (sort of obvious, but nonetheless)

    0 讨论(0)
  • 2021-02-02 07:16

    There's another, non-programmatic external and empirical way to check if you've accidentally left the debug=true attribute on system.web/compilation on a Asp.Net website, i.e. to detect if you've left this configuration on in web.config:

     <system.web>
        <compilation debug="true" targetFramework="xxx"/>
    

    By using a tool such as Fiddler, you can intercept a GET request to your web site, and then change this so that to issue a non-Standard DEBUG HTTP Verb to the site, along with an extra Command: stop-debug Header.

    • Select a GET request to your site and drag it into the Composer
    • Switch to the Raw tab (since DEBUG isn't a standard HTTP verb option)
    • Edit the Verb from GET to DEBUG
    • Add an extra Command: stop-debug Header (above the Cookies), but leave the rest of the Headers and Cookies in place
    • Execute the DEBUG command

    If the Web site returns 200 and the content OK, then you know you've left debug on. The web site will return 403 - Forbidden if debug is off.

    If you've got your website building on a CI/CD build pipeline, best way to turn debug off is to add the following XDT in your Web.Release.config

    <system.web>
      <compilation xdt:Transform="RemoveAttributes(debug)" />
    </system.web>
    

    (Turning debug off in Production is a common security audit requirement)

    0 讨论(0)
提交回复
热议问题