Authentication with firestore for c# desktop app
Asked Answered
E

2

5

I have prepared desktop application which is using firestore database and to get an access to it I have used GOOGLE_APPLICATION_CREDENTIALS. According to the documentation, I was not able to introduce rules for this type of authentication but I would like to do so. I have sent a question to support and I was informed to use REST API. And here is my question ? Is it possible to authenticate via REST API but to read and write data still use Google.Cloud.Firestore nuget ? How to do this ? Please remember that I have desktop app wrote in c# WPF. Maybe you know some other solution for my problem ?

Etruria answered 18/10, 2019 at 12:2 Comment(0)
A
0

From looking at the Github repo for the google-cloud-dotnet libraries it seems they always use GOOGLE_APPLICATION_CREDENTIALS to access Firestore.

In that case there is no way to use the library as is with Firebase Authentication credentials. These issues on the repo also seem to point in that direction.

To access Firestore with Firebase Authentication credentials, you'll have to access it through the REST API in that case.

Alleman answered 18/10, 2019 at 14:40 Comment(0)
E
12

Thank you for feedback Frank, In your link I have found an answer. To authenticate to firestore from desktop app you need to do the following:

public FirestoreDb CreateFirestoreDbWithEmailAuthentication(string emailAddress, 
string password, string firebaseApiKey, string firebaseProjectId)
{
            // Create a custom authentication mechanism for Email/Password authentication
            // If the authentication is successful, we will get back the current authentication token and the refresh token
            // The authentication expires every hour, so we need to use the obtained refresh token to obtain a new authentication token as the previous one expires
            var authProvider = new FirebaseAuthProvider(new FirebaseConfig(firebaseApiKey));
            var auth = authProvider.SignInWithEmailAndPasswordAsync(emailAddress, password).Result;
            var callCredentials = CallCredentials.FromInterceptor(async (context, metadata) =>
            {
                if (auth.IsExpired()) auth = await auth.GetFreshAuthAsync();
                if (string.IsNullOrEmpty(auth.FirebaseToken)) return;

                metadata.Clear();
                metadata.Add("authorization", $"Bearer {auth.FirebaseToken}");
            });
            var credentials = ChannelCredentials.Create(new SslCredentials(), callCredentials);

            // Create a custom Firestore Client using custom credentials
            var grpcChannel = new Channel("firestore.googleapis.com", credentials);
            var grcpClient = new Firestore.FirestoreClient(grpcChannel);
            var firestoreClient = new FirestoreClientImpl(grcpClient, FirestoreSettings.GetDefault());

            return FirestoreDb.Create(firebaseProjectId, null, firestoreClient);
}

Of course firstly you need to add email user in firestore console.

Etruria answered 19/10, 2019 at 16:33 Comment(1)
Requires the nuget package FirebaseAuthentication.netMicrominiaturization
A
0

From looking at the Github repo for the google-cloud-dotnet libraries it seems they always use GOOGLE_APPLICATION_CREDENTIALS to access Firestore.

In that case there is no way to use the library as is with Firebase Authentication credentials. These issues on the repo also seem to point in that direction.

To access Firestore with Firebase Authentication credentials, you'll have to access it through the REST API in that case.

Alleman answered 18/10, 2019 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.