Web API + Firebase Authentication - Have you ever?

Hi guys!

My game is running API calls to a .NET web app i have locally (will migrate it to Cloud Run at a later stage), for the life of me i cannot get the Firebase to see it authenticated, basically the Client (Unity) sends an API call to the web app and the web app tries to access Realtime DB, it all works well if the rules of the DB are open, i can read/write directly from the API any information, but as soon i limit the rules to “auth only” i can no longer access it via the API, but it does work via Client (Unity), what could i possibly be doing wrong?

I will leave here the files if anyone wants to take a peak at them.

FirebaseAuthMiddleware.cs (1.9 KB)
Startup.cs (1.8 KB)
ControllerA.cs (3.5 KB)

Also this is the Docker logs when i do an API call:

2024-10-09 20:50:23 info: Microsoft.Hosting.Lifetime[14]
2024-10-09 20:50:23       Now listening on: http://[::]:8080
2024-10-09 20:50:23 info: Microsoft.Hosting.Lifetime[0]
2024-10-09 20:50:23       Application started. Press Ctrl+C to shut down.
2024-10-09 20:50:23 info: Microsoft.Hosting.Lifetime[0]
2024-10-09 20:50:23       Hosting environment: Production
2024-10-09 20:50:23 info: Microsoft.Hosting.Lifetime[0]
2024-10-09 20:50:23       Content root path: /app
2024-10-09 20:50:34 warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
2024-10-09 20:50:34       Failed to determine the https port for redirect.
2024-10-09 20:50:34 info: MyApi.Firebase.FirebaseAuthMiddleware[0]
2024-10-09 20:50:34       User authenticated: hwqtyfFxf2Z3nqlb3Bve2USu2Or2
2024-10-09 20:50:34 info: MyApi.Controllers.ControllerA[0]
2024-10-09 20:50:34       ProcessData2 called with VariableA: 5, VariableB: 10
2024-10-09 20:50:34 fail: MyApi.Firebase.FirebaseAuthMiddleware[0]
2024-10-09 20:50:34       Error occurred during authentication
2024-10-09 20:50:34       Firebase.Database.FirebaseException: Exception occured while processing the request.
2024-10-09 20:50:34       Url: https://idle-game-e8a28-default-rtdb.europe-west1.firebasedatabase.app/general/aaa/bbb/.json
2024-10-09 20:50:34       Request Data: 
2024-10-09 20:50:34       Response: {
2024-10-09 20:50:34         "error" : "Permission denied"
2024-10-09 20:50:34       }
2024-10-09 20:50:34       
2024-10-09 20:50:34        ---> System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).
2024-10-09 20:50:34          at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
2024-10-09 20:50:34          at Firebase.Database.Query.FirebaseQuery.OnceSingleAsync[T](Nullable`1 timeout)

Any help is much appreciated, thank you :slight_smile:

Extra Info:

When i use Insomnia to test it out, it works well with the token in the link (https://idle-game-e8a28-default-rtdb.europe-west1.firebasedatabase.app/users/hwqtyfFxf2Z3nqlb3Bve2USu2Or2.json?auth=IDtoken) - i get the 200 message

But when i try to pass the token as a header “Bearer IDtoken” with the actual API call (http://localhost:5081/api/ControllerA/ProcessData2) it wont work anymore and it will say that im an unauthorized user - 401 message
If i use this exact same method with open rules in Realtime DB, it will work fine again - 200 message

Any idea?

Cheers

I will just move on and use direct links with ?auth instead and pass all the incoming requests thro a middleware to proof first, guessing this over using headers dont make much of a different for the simple controllers i am needing anyway.