我正在尝试为学校项目创建一个 wpf 应用程序,我认为对我的应用程序实施 firebase 身份验证是个好主意,我找到了一个视频参考网址:https://www.youtube.com/手表?v=aCEOdnd0-so&list=PLA8ZIAm2I03jn4cDxYTqi40DOHjoOYZCz&index=1
尽管我在尝试创建虚拟用户时打开了窗口,但出现错误(无法创建界面 firebaseauthprovider 的瞬间)。我将把代码放入下面的 app.xaml.cs 文件中。如果有人能够找到我的问题的解决方案,那将是一个很大的帮助。谢谢你。
using Firebase.Auth;
using Firebase.Auth.Providers;
using Firebase.Auth.Repository;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Threading.Tasks;
using System.Windows;
namespace Key_Auth.wpf
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private readonly IHost host;
public App()
{
// Build the host
host = Host.CreateDefaultBuilder()
.ConfigureAppConfiguration((hostingContext, config) =>
{
// Load configuration from appsettings.json
config.SetBasePath(Directory.GetCurrentDirectory());
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
})
.ConfigureServices((context, services) =>
{
//get firebase api from configuration
string firebaseApiKey = context.Configuration["FIREBASE_API_KEY"];
services.AddSingleton<FirebaseAuthProvider>((serviceProvider) =>
{
// Create a new FirebaseAuthProvider instance with the API key
//here throws me an error
var authProvider = new FirebaseAuthProvider(new FirebaseConfig(firebaseApiKey));
// Do any additional configuration here, such as setting up a user change event handler
return authProvider;
});
// Register FirebaseAuthProvider as a singleton with a callback
// Register services here
// For example:
// services.AddSingleton<MyService>();
// services.AddTransient<MyOtherService>();
services.AddSingleton<MainWindow>();
// Register MainWindow as a singleton with a callback
services.AddSingleton<MainWindow>((serviceProvider) =>
{
// Get any required dependencies and create the MainWindow instance
var mainWindow = new MainWindow();
// Do any additional configuration here
return mainWindow;
});
})
.Build();
}
protected override async void OnStartup(StartupEventArgs e)
{
/*
base.OnStartup(e);
// Start the host
await host.StartAsync();
// Resolve the main window from the service provider
var mainWindow = host.Services.GetRequiredService<MainWindow>();
// Show the main window
mainWindow.Show();
*/
base.OnStartup(e);
// Start the host
await host.StartAsync();
// Resolve the main window from the service provider
var mainWindow = host.Services.GetRequiredService<MainWindow>();
// Set the main window of the application
MainWindow = mainWindow;
// Show the main window
mainWindow.Show();
//provider.GetService<FirebaseAuthProvider>();
//GetRequiredService<FirebaseAuthProvider>();
}
protected override async void OnExit(ExitEventArgs e)
{
base.OnExit(e);
// Shutdown the host
await host.StopAsync();
host.Dispose();
}
}
}
我想创建一个从 firebase.auth 导入的 FirebaseAuthProvider
转到您的项目 nuget 包管理器,并将您安装的
FirebaseAuthentication.net
包降级到视频教程中使用的确切版本(即版本 3.7.2)。只有这样,您才能创建 FirebaseAuthProvider
的瞬间,并且能够按照教程进行操作。另外,请注意本教程的项目是 WPF
.Net
项目,而不是 .Net Framework
。
更新:
但是,如果您想继续使用最新版本,您可以在 package 的 nuget 网页上查看使用示例
经过大量工作,这是使其工作的代码。阅读下面并理解它。但基本上我们使用的是projectId,firebaseAuthConfig,里面有Apikey,authdomain带来了projectId,我们需要FirebaseAuthProvider的提供者。
我遵循了 Nuget Doc,并且使用的是 Netcore 7 和 Firebase 4.1.0。 该代码是 .Net webApp 中的控制器。
[NonAction]
public async Task<string> SubirStorage(Stream archivo, string nombre)
{
string ApiKey = _firebaseStorage.ApiKey;
string AuthPassword = _firebaseStorage.AuthPassword;
string AuthEmail = _firebaseStorage.AuthEmail;
string StorageUrl = _firebaseStorage.StorageUrl;
string projectId = _firebaseStorage.projectId;
var config = new FirebaseAuthConfig
{
ApiKey = ApiKey,
AuthDomain = $"{projectId}.firebaseapp.com",
Providers = new FirebaseAuthProvider[]{
new EmailProvider()
}
};
var client = new FirebaseAuthClient(config);
var a = await client.SignInWithEmailAndPasswordAsync(AuthEmail, AuthPassword);
var cancellation = new CancellationTokenSource();
var task = new FirebaseStorage(
StorageUrl,
new FirebaseStorageOptions
{
AuthTokenAsyncFactory = async () => await a.User.GetIdTokenAsync(),
ThrowOnCancel = true
})
.Child("imagenes")
.Child(nombre)
.PutAsync(archivo, cancellation.Token);
var downloadURL = await task;
return downloadURL;
}