Firebase authentication in Xamarin.Forms

Firebase Authentication (vert, light)

Integrating Firebase Auth in Xamarin.Forms is very easy and basic authentication flow implementation can be achieved under 20 lines of code. There is more work with settings than code writing. In this article we will:

  1. Configure Firebase app
  2. Create Xamarin.Forms application to authenticate users via Firebase Auth
  3. Create a .NET Core WEB API project to validate Firebase Auth token and return simple data

Firebase Configuration

Firebase has simple and intuitive UI and it is very hard to get lost there, thumbs up for the great work Google! In order to achieve our goal we have to do few simple steps:

  1. Create a new project
    • Add application for Android
      • Download google-services.json
    • Add application for iOS
      • Download GoogleService-Info.plist
  2. Open Authentication section
    • Switch to “Sign-in method” tab
      • Enable “Email/Password”
    • Switch to “Users” tab
      • Add a new user

At this point we should be done with the Firebase configuration and should have all the necessary files in place.

Xamarin.Forms Configuration


  • Add Xamarin.Firebase.Auth NuGet package
  • Import “google-services.json” and set the building action to “GoogleServicesJson”
    • In other words the .csproj should contain the next line: <GoogleServicesJson Include=google-services.json />
  • Make sure that your package name is identical to the package name inside “google-services.json”
  • Add the next line of code in the MainActivity.cs OnCreate before LoadApplication:


  • Add Xamarin.Firebase.iOS.Auth NuGet package to iOS project
  • Import “GoogleService-Info.plist” and set the building action to “BundleResource”
  • Make sure that your bundle identifier is identical to the bundle identifier inside “GoogleService-Info.plist”
  • Add the next line of code in the AppDelegate.cs FinishedLaunching before LoadApplication:
  • Please note that if you want to test on the iOS simulator you will have to do one extra step as described here due to a bug.


Since there are only platform specific Xamarin.Firebase NuGet packages, we will have to create a simple abstraction layer that will look like this:

public interface IFirebaseAuthenticator
    Task<string> LoginWithEmailPassword(string email, string password);

Each platform will have to implement this interface separately.
Android implementation:

public class FirebaseAuthenticator : IFirebaseAuthenticator
    public async Task<string> LoginWithEmailPassword(string email, string password)
        var user = await FirebaseAuth.Instance.
email, password);
        var token = await user.User.GetIdTokenAsync(false);
        return token.Token;

iOS implementation:

public class FirebaseAuthenticator : IFirebaseAuthenticator
    public async Task<string> LoginWithEmailPassword(string email, string password)
        var user = await Auth.DefaultInstance.SignInAsync(email, password);
        return await user.GetIdTokenAsync();

The implementations are almost identical and in both cases are 2-3 lines of code to authenticate a user and return a token.

After obtaining a token we could use it to access our protected API:

using (var httpClient = new HttpClient { DefaultRequestHeaders = { Authorization = token } })
    var res = await httpClient.GetAsync(;);
    var content = await res.Content.ReadAsStringAsync();

.NET Core WEB API Configuration

Now we want to validate the Firebase token we obtained in our Xamarin.Forms app. For this, we have to setup and enable Authentication in Startup.cs:

public void ConfigureServices(IServiceCollection services)
    var firebaseProjectId = “SET_ME_UP”;
    .AddJwtBearer(options =>
        options.Authority = $”{firebaseProjectId};
        options.TokenValidationParameters = new TokenValidationParameters
            ValidateIssuer = true,
            ValidIssuer = $”{firebaseProjectId},
            ValidateAudience = true,
            ValidAudience = firebaseProjectId,
            ValidateLifetime = true
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    if (env.IsDevelopment())

    // Order matters

The code is self explanatory, but if you would like to dive into details there is a great article explaining the code above.


Integrating Firebase to Xamarin.Forms and .NET Core WEB API is simple and fun. While this example was pretty ordinary you could extend it to support other Firebase Auth flows as a homework.

Code example is available on github.

16 thoughts on “Firebase authentication in Xamarin.Forms

  1. How do I add Facebook and Email as both options to login ? ( Making sure that the id is the same across ? )


  2. Hi Evgeny, thanks for the great article. You mention the FirebaseUI auth at the end of your post, but I can’t seem to find a supporting Xamarin library for this. Do you know if FirebaseUI is possible with Xamarin Forms?


  3. Just thought I would share for anybody that runs into this issue. Authentication with the ios simulator requires keychain access. This for whatever reason requires at least ONE provisioning profile exist on your system which in turn requires a physical iphone to create. Once you’ve got a provisioning profile the fix mentioned above will work. It just seems counter intuitive but I thought I would mention for those of us using android devices for our personal phones. If you have somebody willing to lend an iphone for creating a provisioning profile through the apple developer tool(paid subscription…) then it should work.
    Otherwise thanks for a great tutorial. Really helpful in getting things started.


  4. Hi Evgeny

    Thanks for the clear steps.
    How would I go about extending this to use a refresh token for background/auto re-authentication since the access token expires after 60minutes?



  5. Hi Evgeny,
    when I try to install Xamarin.Firebase.Auth I get the follwing error:
    Package Xamarin.Firebase.Auth 71.1510.0-preview1 is not compatible with netstandard2.0 (.NETStandard,Version=v2.0).
    This is also the case for the latest stable version.
    Do you have a workaround for that?


    1. Hello,
      You must be trying to install the package for the PCL project. You need to install the packages to platform projects only and work around with an interface as explained in the post.


  6. nvm found out but i need a oncomplete listener or it lets them go to the next activity whether their credentials are valid or not


  7. Hey Evgeny, I’m implementing PhoneAuthentication using firebase following your article. The user gets successfully verified, and I’m able to get user details. But while getting token “var token = await user.GetIdTokenAsync(true);”, I receive a NullPointerException. Any idea why this could be? Am I missing something?


  8. Hey Evgeny, I was implementing Firebase Phone auth following your article. The user successfully authenticates and I’m able to get the User details. But while getting Id Toke “var token = await user.GetIdTokenAsync(true);”, it throws me a null pointer exception. Any idea why this could be? Am I missing something?


  9. Hi!
    Thanks for the article, I am new to firebase, currently on a 30-day no authentication rule needed anyone with URL can update my database. so I got my development app hook up to firebase db, I created my on “custom authentication”, basically a table for person with login id, password then check against that. everything works as plan. Now question is, if I continue using this method, does that mean I have to hardcode my google email/password inside the app, so that my app can authenticate against firebase to gain access my realtime database? I am a bit at loss. Thanks for the helps!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s