0
votes

I'm using Firebase authentication for my existing web API created on ASP.NET Core 3.1. I have to verify firebase user id token inside GetUserId method of IUserIdProvider interface.

Server Side Codes:

public class CustomUserIdProvider : IUserIdProvider
{
     
    public string GetUserId(HubConnectionContext connection)
    { 

        /* Getting token from header when a SignalR client is connecting */
        string FirebaseUserIDToken = connection.GetHttpContext().Request.Headers["token"];

        /* Decrypting firebase id token */
        FirebaseToken firebaseToken = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(token);

        /* assigning firebase auth user id to SignalR user */
        return firebaseToken.Uid;

    }
}

the problem is await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(token) is Asynchronous function and GetUserId does not support Asynchronous calls.

Client Side Codes:

hubConnection = HubConnectionBuilder.create("<my server>/testHub").withHeader("token", <firebase id token>).build();
    

For security reasons I can not simply attached firebase UID in the header instead of firebase id token.

1

1 Answers

0
votes

I have found the solution,

Task<FirebaseToken> task = FirebaseAuth.DefaultInstance.VerifyIdTokenAsync("<ID Token Here>");
task.Wait(); //this did the trick.
string uid = task.Result.Uid;

If you want to verify if the task is successfully completed, use task.IsCompletedSuccessfully property.