Detect xmlhttprequest call?!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Nemtsev [MVP]

    Detect xmlhttprequest call?!

    Hello,

    In the current application we have several httpmodules to process pages,
    and we have several pages with calls directly through the XMLHttpRequest.
    Now the requiremens is to detect the calls through the XMLHttpRequest and
    apply addional actions in httpmodule for these types of call.

    Because they dont use ASP.NET AJAX for this I cant use ScriptManager.I sInAsyncPostBac k.
    Everything what comes into my my how to catch these calls is check CurrentContext. Request.Content Type
    for the string entry which is set like
    request.setRequ estHeader("Cont ent-Type", "applicatio n/x-www-form-urlencoded");

    before making XMLHttpRequest call. If !(string.IsNull OrEmpty(Current Context.Request .ContentType))
    then we have XMLHttpRequest call.

    I wonder if there are others way to check if call made via XMLHttpRequest? !

    ---
    WBR,
    Michael Nemtsev [Microsoft MVP] :: blog: http://spaces.live.com/laflour

    "The greatest danger for most of us is not that our aim is too high and we
    miss it, but that it is too low and we reach it" (c) Michelangelo


  • =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=

    #2
    RE: Detect xmlhttprequest call?!

    Hi Michael,

    Use Headers collection to identify request comes from a cetrain source.
    That's the way Microsoft AJAX does it:
    Headers collection:

    [0] "Cache-Control" string
    [1] "Connection " string
    [2] "Content-Length" string
    [3] "Content-Type" string
    [4] "Accept" string
    [5] "Accept-Encoding" string
    [6] "Accept-Language" string
    [7] "Host" string
    [8] "Referer" string
    [9] "User-Agent" string
    [10] "x-microsoftajax" string <-- AJAX
    because you are in control what you put into headers when issueing request
    through XMLHttpRequest.

    HTH
    --
    Milosz


    "Michael Nemtsev [MVP]" wrote:
    Hello,
    >
    In the current application we have several httpmodules to process pages,
    and we have several pages with calls directly through the XMLHttpRequest.
    Now the requiremens is to detect the calls through the XMLHttpRequest and
    apply addional actions in httpmodule for these types of call.
    >
    Because they dont use ASP.NET AJAX for this I cant use ScriptManager.I sInAsyncPostBac k.
    Everything what comes into my my how to catch these calls is check CurrentContext. Request.Content Type
    for the string entry which is set like
    request.setRequ estHeader("Cont ent-Type", "applicatio n/x-www-form-urlencoded");
    >
    before making XMLHttpRequest call. If !(string.IsNull OrEmpty(Current Context.Request .ContentType))
    then we have XMLHttpRequest call.
    >
    I wonder if there are others way to check if call made via XMLHttpRequest? !
    >
    ---
    WBR,
    Michael Nemtsev [Microsoft MVP] :: blog: http://spaces.live.com/laflour
    >
    "The greatest danger for most of us is not that our aim is too high and we
    miss it, but that it is too low and we reach it" (c) Michelangelo
    >
    >
    >

    Comment

    • Anthony Jones

      #3
      Re: Detect xmlhttprequest call?!

      "Michael Nemtsev [MVP]" <nemtsev@msn.co mwrote in message
      news:900895ec59 2018cabcd91c37a 570@msnews.micr osoft.com...
      Hello,
      >
      In the current application we have several httpmodules to process pages,
      and we have several pages with calls directly through the XMLHttpRequest.
      Now the requiremens is to detect the calls through the XMLHttpRequest and
      apply addional actions in httpmodule for these types of call.
      >
      Because they dont use ASP.NET AJAX for this I cant use
      ScriptManager.I sInAsyncPostBac k.
      Everything what comes into my my how to catch these calls is check
      CurrentContext. Request.Content Type
      for the string entry which is set like
      request.setRequ estHeader("Cont ent-Type",
      "applicatio n/x-www-form-urlencoded");
      >
      before making XMLHttpRequest call. If
      !(string.IsNull OrEmpty(Current Context.Request .ContentType))
      then we have XMLHttpRequest call.
      >
      I wonder if there are others way to check if call made via
      XMLHttpRequest? !
      >
      Its not possible to guarantee that the request is coming from XMLHttpRequest
      unless there is some help from the client side. You are using a pattern of
      usage which you hope is not also created in normal use of your app. Unless
      you are prepared to change they way the clientside makes these requests
      (and that assumes you have control over that) then your approach is probably
      the only one available to you.


      AJAX adds a header to specifically identify the request as has already been
      pointed out.


      --
      Anthony Jones - MVP ASP/ASP.NET


      Comment

      Working...