1

If I created an Azure Function in isolate process mode, HttpRequestData.Cookies is empty. If I create exactly the same Function in non isolated mode it has cookies.

To illustrate my point. I created 2 functions. Both running on the same port (they are run independently).

Isolated Process Function

Non Isolated Process Function enter image description here

(The functions were created with default VS 2021 templates and the only changes that was made is the function name)

From blazor the functions are simply called with. enter image description here

In chrome I can see the cookies being passed: enter image description here

(I started this journey trying to inject the ClaimsPrincipal as discussed in: https://www.youtube.com/watch?v=eZQq3zw3WL4. In an Isolated Azure function this value is null, and once again not in an Non Isolated Azure function. I'm assuming that this is because the auth cookie is not picked up)

EDIT: The accepted answer helps with fixing the empty claims issue. However even when the claims are present we don't get the ClaimsPrincipal injected. I found this article that describes a workaround: https://adamstorr.azurewebsites.net/blog/using-azure-functions-middleware-to-access-claimsprincipal-in-azure-static-web-apps

Murdock
  • 4,352
  • 3
  • 34
  • 63

1 Answers1

1

.Net 5 Isolated Http Trigger Azure Function:

enter image description here

Non-Isolated Function: .Net 3.1 Http Trigger Azure Function

enter image description here

As I observer a behavioral difference between In-Process (Non Isolated) and Out-Process (Isolated Environment), i.e., in the function declaration uses HttpRequest/ObjectResult in Non-Isolated and HttpRequestData/HttpResponseData in Isolated.

As given in the HttpRequestData Class Microsoft Documentation, it gets the cookies of the request class. Where as HttpRequest Or the ObjectResult classes are ASP .Net Core features to store the field like body, headers, cookies, content-type, content-length etc.,.

And the main usecase of Azure Functions are stateless. You can probably emulate it yourself by using Request.Cookies and Response.Cookies but ASP .NET features aren't directly applicable in Functions.

In the December Month of GitHub Issues Page of azure-functions-dotnet-worker, it is marked as a bug in the Azure Functions v3 that the HttpRequestData.Cookies is Empty but can be resolved when we specify the Cookies class from the request object or it can be resolved when we update the Microsoft.Azure.Functions.Worker to 1.6.0 version.

enter image description here

Reference: https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide#differences-with-net-class-library-functions

  • > "can be resolved when we specify the Cookies class from the request object " < What do you mean by this? – Murdock Jan 26 '22 at 08:54
  • Thanks for your answer. I can report that upgrading the Worker nuget adds the claims. However the ClaimsPrincipal is still null (unlike the non isolated process). Any Ideas? – Murdock Jan 26 '22 at 08:55
  • Hello @Murdock, >"can be resolved when we specify the Cookies class from the request object " - This means I have added the cookies code to get the cookies data using `req object from HttpRequestData class` in the Isolated Azure Function. –  Jan 26 '22 at 09:00
  • Not following. The cookies are not on the HttpRequestData. Where does the cookies then come from if not from the HttpRequestData? – Murdock Jan 26 '22 at 09:13
  • If cookies not comes from [HttpRequestData](https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.functions.worker.http.httprequestdata.cookies?view=azure-dotnet) class, then we can create and call the cookies manually. Please refer the example from this [SO Thread of Creating cookies in Azure Functions](https://stackoverflow.com/questions/61630263/how-to-create-sessions-and-cookies-in-azure-functions). –  Jan 26 '22 at 09:24
  • Sure, but the browser is sending the cookies. So how would THE FUNCTION get access to the cookies sent via the browser? – Murdock Jan 26 '22 at 09:30
  • Hello @Murdock, Hope this [existing SO Answer](https://stackoverflow.com/questions/60816136/client-fails-to-show-cookie-sent-by-azure-function-app-in-browser-storage) helps to get the cookies access from the browser! –  Jan 26 '22 at 09:34
  • It does not. That answer shows how to do the reverse. Add a cookie in the function that shows up in the browser. – Murdock Jan 26 '22 at 09:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241416/discussion-between-harikrishnarajoli-mt-and-murdock). –  Jan 26 '22 at 09:37