通过firestore验证c#桌面应用程序

通过firestore验证c#桌面应用程序,c#,google-cloud-firestore,desktop-application,C#,Google Cloud Firestore,Desktop Application,我已经准备好了使用firestore数据库的桌面应用程序,为了访问它,我使用了GOOGLE_应用程序_凭据。根据文档,我无法为这种类型的身份验证引入规则,但我希望这样做。我已经向技术支持人员发送了一个问题,并被告知使用RESTAPI。这是我的问题?是否可以通过REST API进行身份验证,但仍然可以使用Google.Cloud.Firestore nuget读取和写入数据?如何做到这一点?请记住,我有一个用c#WPF编写的桌面应用程序。也许你知道我的问题的其他解决方案吗?看一下,他们似乎总是使用

我已经准备好了使用firestore数据库的桌面应用程序,为了访问它,我使用了GOOGLE_应用程序_凭据。根据文档,我无法为这种类型的身份验证引入规则,但我希望这样做。我已经向技术支持人员发送了一个问题,并被告知使用RESTAPI。这是我的问题?是否可以通过REST API进行身份验证,但仍然可以使用Google.Cloud.Firestore nuget读取和写入数据?如何做到这一点?请记住,我有一个用c#WPF编写的桌面应用程序。也许你知道我的问题的其他解决方案吗?

看一下,他们似乎总是使用
GOOGLE\u应用程序\u凭据来访问Firestore

在这种情况下,无法使用Firebase身份验证凭据的库。这些似乎也指向了这个方向


要使用Firebase身份验证凭据访问Firestore,在这种情况下,您必须通过REST API访问它。

感谢您的反馈,Frank,在您的链接中,我找到了答案。要从桌面应用程序验证firestore,您需要执行以下操作:

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);
}

当然,首先需要在firestore控制台中添加电子邮件用户。

需要nuget包FirebaseAuthentication.net