Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question UnityWebRequest with including httpOnly cookies

Discussion in 'Web' started by saberpieter, Sep 1, 2023.

  1. saberpieter

    saberpieter

    Joined:
    Sep 1, 2023
    Posts:
    1
    Hi all,

    I have an WebGL application which needs to integrate with the existing authentication system at my work which uses session cookies to authenticate requests.

    I have not been able to find a way to configure Unity to enable the
    credentials: "include"
    option which is typically set on JavaScript fetch requests to instruct the browser to include cookies in the request.

    From some older resources I found online, the advice stated that this was impossible to enable natively within Unity, and suggested using a 'hack' which involved overriding the XMLHttpRequest object to enable
    withCredentials: true
    by default, thus ensuring that the requests from Unity include cookies once they are converted for WebGL.

    I've also learned that since version 2021.3, Unity now uses the Fetch API to make WebGL network requests. Thus, the previously suggested override hack no longer works.

    What is the recommended approach for making requests from Unity that include httpOnly cookies automatically using the credentials option?
     
  2. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    951
    Extending on this feature is unfortunately not currently supported.

    It should be possible to solve the issue by monkey patching fetch() API to inject the custom credentials.

    Alternatively, you can try copying the FetchWithProgress.js file into your project as a "FetchWithProgress.prejs" file, and then modifying the implementation of fetch() there to post the custom credentials. I believe that should allow overriding the default functionality.