Firebase - Create user using FirebaseAdmin in ASP.NET CORE 3.1

Create or Register user in firebase using the Firebase Admin SDK in ASP.NET CORE 3.1

September 7, 2020

In this article we will cover how to create firebase user using the Firebase Admin SDK in ASP.NET Core 3.1.

Prerequisites:

Understanding of ASP.NET Core 3.1 and Firebase

Let's Start:

The first step Let's create a fresh Asp.Net Core project and add below mentioned dll as reference from Nuget Package.

FirebaseAdmin

appsettings.json

Copy below mentioned code and replace it with existing code in appsettings.json.

{
  "Logging": {
    "LogLevel": {
      "Default""Information",
      "Microsoft""Warning",
      "Microsoft.Hosting.Lifetime""Information"
    }
  },
  "Firebase": {
    "type""service_account",
    "project_id""your_project_id",
    "client_email""your_client_email",
    "client_id""your_client_id",
    "private_key""your_private_key"
  }
}

Refer Notes mentioned below to get all the keys mentioned inside appsettings.json related to firebase.

Model

Create Model named "Firebase.cs" and add below mentioned Properties.

namespace HpBlogs.Models
{
    public class Firebase
    {
        public string type { getset; }
        public string project_id { getset; }
        public string client_email { getset; }
        public string client_id { getset; }
        public string private_key { getset; }
    }
}

FirebaseController.cs

Create Controller named "FirebaseController.cs" and replace it with below mentioned code.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Microsoft.Extensions.Configuration;
using System;
using FirebaseAdmin;
using Google.Apis.Auth.OAuth2;
using FirebaseAdmin.Auth;

namespace HpBlogs.Controllers
{
    public class FireBaseController : Controller
    {
        IConfiguration _config { getset; }
        public FireBaseController(IConfiguration configuration)
        {
            _config = configuration;
        }

        public async Task<IActionResultIndex()
        {
            try
            {
                // Get Firebase Configuration from appsettings.json and Bind to Firebase object
                Firebase fbconfig = new Firebase();
                _config.Bind("Firebase"fbconfig);

                var json = JsonConvert.SerializeObject(fbconfig);
                FirebaseApp.Create(new AppOptions()
                {
                    Credential = GoogleCredential.FromJson(json),
                });
            }
            catch { }

            // User Data with valid details
            UserRecordArgs args = new UserRecordArgs()
            {
                Email = "emailadddress@gmail.com",
                EmailVerified = false,
                PhoneNumber = $"+911234567890",
                Password = "password",
                DisplayName = "FullName",
                Disabled = false,
            };

            try
            {
                // Create User
                UserRecord createdUser = await FirebaseAuth.DefaultInstance.CreateUserAsync(args);
            }
            catch (Exception ex)
            {
                // check for any Exception like invalid email or mobile number
            }
            return View();
        }
    }
}

Run the project and check if the user gets created successfully or not.

Notes:

1. I assume you have already registered on Firebase and created project. If you haven't please do this by using below link.

https://console.firebase.google.com/

2. You will find all details from below mentioned info.

project_id - https://console.firebase.google.com/project/your-project-id/settings/general

client_email - (Firebase service account) https://console.firebase.google.com/project/your-project-id/settings/serviceaccounts/adminsdk

client_id - (Click on firebase-adminsdk link.) https://console.cloud.google.com/iam-admin/serviceaccounts/project?project=your-project-id&authuser=0

private_key - (Click on Generate new private key button.) https://console.firebase.google.com/project/your-project-id/settings/serviceaccounts/adminsdk

3. Now we have everthing which required to create user using the firebase admin.

4. To know how to update user using firbase admin Click here

5. To know how to delete user using firbase admin Click here

Additional References:

1. https://firebase.google.com/docs/admin/setup

2. https://firebase.google.com/docs/auth/admin/manage-users#c

Post Comments(0)

Leave a reply

Will not be displayed in comment box .

Loading...