Connected successfully



Authenticating Your C# Application with the Firebase Database



In todays post we will be going through one of the ways we can have some form of basic authentication implemented in to any project. This form of authentication can be applied wherever you want, whether it be you are Posting, Patching, Deleting and more. Keep your project safe and allow only those you want to have permission to access data within your database.

We will begin by explaining what is being used in this example. Within our project we are first setting up some authentication rules for read and write permission to the firebase database. This means that any requests made to the database will be denied as they are unauthorised. In order to have these requests authorised the databases secret key will need to be made use of within the HTTP request.

To set the authentication rules you will first need to head in to the firebase project click on the Database button and then select the Rules tab, by default if you have created your project in test mode both the read and write functions will be set to true and will be something similar to that seen in the below snippet of code

Before continuing to read on wards I would strongly advise to read on the previous articles working through the basics of C# and Firebase which can be found below:
Inserting data in to the Firebase database
Updating data in the Firebase database
Deleting data in Firebase database
Retrieving all data from the Firebase database
Removing random base-64 keys from the parent nodes within the Firebase database
Retrieving Individual parent and child nodes from the Firebase database





We want to change both of the above read and write to false instead meaning that no one will have the permission to read or write to the database project, once you have done this your rules will be seen exactly to the found in the below snippet of code





Firebase includes within all its projects a database secret, although Firebase no longer promotes its use and states that this feature is now deprecated it is still be supported. Firebase now promotes the use of legacy tokens and other methods. In this post however we will be going through how make use of the database secret and were it can be found.

The database secret can be found by clicking on the Settings cog within your project and then selecting Project Setting. We then want to click on the Service accounts tab and select Database secrets on the main page. Once this has been clicked on you will be presented with the database secret for your project. If you are trying to create an application in C# that will be handling important information I wouldnt advise on handing this secret around. The database secret will look something like the below





We then need to copy this database secret and attach it to HTTP web request URL that is being used within our C# application. By not having that database secret included within the URL whilst the new rules seen above are in place you can expect a lot of 401 exceptions to be thrown telling you the request made has been unauthorised. There are many ways in which you can have the database secret added to the URL however in this example we are going for the easiest which is pasting it in to the actual code, once you have done this your HTTP web request URL should look like the following.





Looking at the above we can see that there is no major change within the URL other than it being followed by ?auth= and then the database secret. Be sure that HTTP web request URL matches the format exactly or you may encounter some issue along the way.

If you are not yet familiar with the above code it is suggest you read up on some of the previous articles a list of which can be found below:
Inserting data in to the Firebase database
Updating data in the Firebase database
Deleting data in Firebase database
Retrieving all data from the Firebase database
Removing random base-64 keys from the parent nodes within the Firebase database
Retrieving Individual parent and child nodes from the Firebase database

Some example source code has been included in the below in order to display as to how this all may come together to form a project. The project we have implemented it in to is the updating of child nodes via use of the current hour, minute and second. Be sure that you have installed the Newtonsoft.Json package to your C# application for this whole project to work. Also show your support by liking, subscribing to the YouTub channel and sharing all the relevant links.