FirebaseUI for .NET (WPF + UWP + Xamarin)

Tomáš Bezouška
Step Up Labs
Published in
5 min readJan 17, 2020

Some time ago I wrote a post about Firebase Authentication C# library which allows developers add support for Firebase sign-in in their C# based applications. The library has since been downloaded more than 100k times from nuget which is pretty wild. We use it internally in our UWP version of Settle Up — app for managing group expenses — and it works just fine.

One thing that is somewhat annoying though is the fact that C# developers are still required to do a lot of work that their fellow Android/iOS/Web colleagues don’t have to. In C#, you need to manually sign in with Facebook/Google/etc., get the access token and then sign in with Firebase (sure, there are other libraries to do that, but it’s still extra work). For signing in with Email & Password it’s even worse: you need to create the whole UI flow yourself (sign in / sign up / forgotten password / etc.). On other platforms, developers can use FirebaseUI which takes care of everything. Wouldn’t it be cool if C# developers also had such a library? So I wrote it.

FirebaseUI for WPF

Platform support

The situation in the .NET ecosystem is not as simple as it is on other platforms — you cannot simply have a single UI library because there are multiple UI frameworks. That’s why the C# implementation actually comprises 4 libraries:

  • FirebaseAuthentication.net
  • FirebaseAuthentication.WPF
  • FirebaseAuthentication.UWP (not finished)
  • FirebaseAuthentication.Xamarin (not finished)

FirebaseAuthentication.net is the original, platform independent (.NET Standard 2.0) authentication library, rewritten from scratch. Its new API is now more in line with the official JavaScript SDK, which I drew inspiration from, and allows the UI libraries to be built on top of it.

The other libraries are framework-specific, as can be told by the suffix in their names. The only one that currently exists is for WPF targeting .NET Core 3.1. Other platforms (UWP and Xamarin) are currently on the roadmap.

FirebaseAuthentication.net

Enough talk, let’s see some code. First, let’s look at the new base library in action, which can be used in any framework which supports .NET Standard 2.0.

Configure and initialize FirebaseAuthClient

Configuration of the FirebaseAuthClient is pretty straightforward, you specify your API Key, Auth Domain (both can be found in Firebase Console) and your desired auth providers. Also, notice the UserRepository which specifies where to persist the user’s credentials. By default, the library uses in-memory repository, so to preserve user’s credentials between runs, use FileUserRepository.

After you have your client, you can sign in or sign up the user with any of the configured providers.

Use FirebaseAuthClient to sign in / create user

As you can see, the sign-in methods give you a UserCredential object, which contains (among other things) a User object, which holds details about a user as well as some useful methods. For example, use GetIdTokenAsync() to get a valid IdToken you can use as an access token to other Firebase API (e.g. Realtime Database).

User object has user details and other methods

FirebaseUI

The platform-specific UI libraries use the FirebaseAuthClient under the hood, but need to be initilized via the static Initialize method of FirebaseUI.

FirebaseUI comes with FirebaseUIControl which installs into your Visual Studio toolbox and can be used in your xaml as follows (WPF):

This gives you the design-time preview below. Note that the designer will always show only these three providers, but when you run your app it will correctly show those you configure.

FirebaseUIControl in Visual Studio

Toggling the visibility of this UI control is up to you, depending on your UI framework (WPF/UWP/Xamarin) and your business logic. For example, you could show it as a popup or a Page inside a Frame etc.
In general, though, you will typically want to toggle the control’s visibility in response to the AuthStateChanged event:

Toggle visibility of FirebaseUIControl based on AuthStateChanged

Sign in with third party identity providers

Currently, the library supports signing in with Apple, Google, Facebook, Twitter, Github and Microsoft identity providers. The UI library shows a dialog with an embedded WebView where the user signs in.

Sign in via Microsoft

Email and password sign in

While the other auth providers simply open a dialog with an embedded WebView, email and password is a somewhat special case which needs an entire UI flow to be built around it. The library has 4 screens for sign in, sign up and password recovery.

Email & password provider

Localization

Since the Android FirebaseUI is open-source I was able to pull their localized string resources and transform them into .NET resource files with powershell (something I already blogged about). Therefore, the library supports almost 40 languages out of the box. It automatically uses the appropriate one based on the values of DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture. You can manually change the language as follows:

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("cs");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("cs");
FirebaseUI localization

What’s next?

The library is still under active development. It is open-source, hosted on github. As mentioned previously, FirebaseUI is currently only available for WPF on .NET Core 3.1, but UWP and Xamarin are on the roadmap.

Currently, you can install v4.0.0-alpha.1 from nuget. If you’re interested in nightly builds, see github for more details.

# base package
dotnet add package FirebaseAuthentication.net -v 4.0.0-alpha.1

# Platform specific FirebaseUI (has dependency on base package)
dotnet add package FirebaseAuthentication.WPF -v 4.0.0-alpha.1

Sample projects are also available on github. Feel free to clone the whole repo and try them out.

If you have any issue, comments or feature requests, please raise them in this github issue.

--

--