Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take two weeks Trial! Try Now

Resolve an 'Application Default Credentials' error when accessing GCP resources through the ASP.NET application

Introduction

When working with Google ASP.NET MVC 4 applications or C # libraries, to leverage the Google Cloud Platform computer infrastructure as a platform, most users encounter a common error, 'The Application Default Credentials are not available' or 'Environment variable,' GOOGLE_APPLICATION_CREDENTIALS must be defined '. The only reason for this error is that we need to authorize our application for our own Google project. Let us understand and fix this with an example.

Prerequisites

1) Google Cloud Platform Account - (You can get your subscription by clicking, (https://goo.gl/e5KNLF)

2) Google Cloud SDK - Download at https://goo.gl/Sxz6h8

Resolving with example

Now, create a new Google ASP.NET 4 MVC application, and provide it a suitable name as shown below. Click Ok.

img

Select Google Cloud Platform project that will run this application. You can enter a project ID or select from the existing project being listed. Once selected Click Ok.

img

It will create a basic Google ASP.NET MVC application structure, as seen below image.

img

Now, let’s build & run the application. You will encounter an error at filterConig.cs file.

img

A readable image of Error,

img

So as mentioned above this error occurs as we need to authorize an application to Google Project.

Google Application Default Credentials

The preferred or most recommended option to achieve this is to take advantage of Google Application Default Credentials through the gcloud utility.

The gcloud utility is part of the Google Cloud SDK. While installing Cloud SDK, make sure you check the entry mentioning Beta Component, to have this component installed in the machine. The reason, command used to configure Google Application Default Credentials is still in beta. (At the time of writing this article).

If not selected, no worries, we can still have it through running Google Cloud Shell command, gcloud components install beta This command will install beta components manually. Now open Google Cloud SDK Shell, being installed in your machine. And make sure you open it with ‘Run as administrator.

img

Now run the following beta command, as shown, gcloud beta auth application-default login. This command will open the browser to start the authentication process.

img

Select the Google credentials associated with your project and login

img

And Click on Allow. It will authorize our machine to access projects via the credentials we opted for. So the best part, it’s a one-time activity, and we need not require to repeat this again n again for different projects.

img

Once done, it will lead to the below page mentioning authentication success.

img

Now the machine is set up to access Google Cloud Platform resources, being enabled for the application. The file with associated credentials is downloaded to our machine. Refer to the below image, it mentions the file details.

img

Once done with this process, now run the application. Now it will work fine!

img

Above Setup will not only fix the error, but will make you ease to deploy application to either Google App Engine and Google Cloud Engine, without managing any config files or settings for different environments., as both the services are configured to use same credentials.

Revoking Credentials

You must be thinking, we have authorized the entire machine to use our credentials. What if we want to deauthorize or revoke this credential? Well revoking these credentials is as simpler as it was adding. Just run the below command in Cloud Shell, gcloud auth application-default revoke It would prompt for continuing the revoke process, enter ‘Y’.

img

Once the revoke is done, it will display a message saying ‘Credentials Revoked’. It will delete the earlier downloaded credential file. This revoke will not affect any environment variables or any other active account credentials on Google VM.

img

Now once again try running the Google ASP.NET MVC application we created. And it will throw an error, confirming credentials are revoked!

img

So with a few easy steps using gcloud, we authenticated our Google ASP.NET MVC C# application with default credentials. Also using the same we revoked credentials of ASP.NET Application Development. While there are other methods of adding this credential, using the steps above is the easiest with the most recommended option.

Recent Blogs

Categories

NSS Note

Some of our clients

team