Firebase Auth User Management Programatically from C# [migrated]
I'm trying to manage users on Firebase Auth from my code. I was following the documentation: https://cloud.google.com/identity-platform/docs/admin/manage-users
This is the code I made:
public async System.Threading.Tasks.Task<int> DeleteAsync(string email)
{
try {
GoogleCredential cred = GoogleCredential.FromFile("path\\to\\downloadfile\\Mysandboxproject.json");
FirebaseApp.Create(new AppOptions()
{
Credential = cred,
ServiceAccountId = "[email protected]",
});
//Get User To Remove:
UserRecord userRecord = await FirebaseAuth.DefaultInstance.GetUserByEmailAsync(email);
await FirebaseAuth.DefaultInstance.DeleteUserAsync(userRecord.Uid);
return 1;
}
catch (Exception ex)
{
throw ex;
}
}
But when it goes to the line where it goes to GetUserByEmailAsync it never returns... doesn't throw exception, nor returns the user, of course the user is never deleted. What am I doing wrong? am I missing something related with the conection or configuration of firebase comunication? Thanks in advance.
Solution 1:
I'm unable to repro your issue; the following works for me:
PROJECT=[[YOUR-PROJECT]] # Containing Firebase Auth
ACCOUNT=[[YOUR-ACCOUNT]] # Service Account name
EMAIL="${ACCOUNT}@${PROJECT}.iam.gserviceaccount.com"
gcloud iam service-accounts create ${ACCOUNT} \
--project=${PROJECT}
gcloud iam service-accounts key create ${PWD}/${ACCOUNT}.json \
--iam-account=${EMAIL}
gcloud projects add-iam-policy-binding ${PROJECT} \
--member=serviceAccount:${EMAIL} \
--role=roles/firebaseauth.admin
export GOOGLE_APPLICATION_CREDENTIALS=${PWD}/${ACCOUNT}.json
With C#:
using System;
using System.Threading;
using System.Threading.Tasks;
using FirebaseAdmin;
using FirebaseAdmin.Auth;
using Google.Apis.Auth.OAuth2;
namespace app
{
class Program
{
static async Task Main(string[] args)
{
FirebaseApp.Create();
var email = "[[EMAIL ADDRESS]]";
var record = await FirebaseAuth.DefaultInstance.GetUserByEmailAsync(email);
Console.WriteLine($"Successfully fetched user data: {record.Uid}");
}
}
}
Then:
dotnet new console
dotnet add package FirebaseAdmin --version 2.2.0
dotnet run