Azure Key Vault & Access from C#

Azure Key Vault & Access from C#

In this article we can explore how to create an Azure Key Vault & Access from C#.

Azure Key Vault

Azure Key Vault allows to keep encrypted secured strings. Eg: Connection Strings, Passwords etc.

Create Azure Key Vault

Open Azure Portal & Create a new Key Vault as shown below.

image

Go the Secrets blade and create a new Secret with name as key1 and value as value1

image

Create App Registration

We need to create an App Registration for our Console Application. This will enable to Authenticate our Console Application using the Credentials.

Go to Azure Portal > Azure Active Directory > App Registrations. Create new App Registration as below.

image

Create new client secret too.

image

Now copy the Client ID and Client Secret which you need in the next steps.

Authorize Console Application

We need to Authorize the Console Application to the Key Vault. Without this step you will get Forbidden error.

Go to Key Vault > Access Policies blade

image

Click Add Access Policy and select our Console Application as Principal.

image

image

Now onwards any application authenticated through Client Credentials of the Console App Registration will be considered as Principal – which is the Security Identity for the application.

Create Project

Create a new console application in Visual Studio. Add references to following:

· Microsoft.Azure.KeyVault

· Microsoft.IdentityModel.Clients.ActiveDirectory

Replace the code with following.

using Microsoft.Azure.KeyVault;

using Microsoft.IdentityModel.Clients.ActiveDirectory;

using System;

using System.Threading.Tasks;

namespace KeyVault

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine($”Secret Value from Vault is: {GetVaultValue()}”);

Console.ReadKey(false);

}

static string GetVaultValue()

{

KeyVaultClient client = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetToken));

var vaultAddress = “https://your-key-vault.vault.azure.net”;

var secretName = “key1”;

var secret = client.GetSecretAsync(vaultAddress, secretName).GetAwaiter().GetResult();

return secret.Value;

}

static async Task<string> GetToken(string authority, string resource, string scope)

{

var clientId = “YOUR CLIENT ID”;

var clientSecret = “YOUR CLIENT SECRET”;

ClientCredential credential = new ClientCredential(clientId, clientSecret);

var context = new AuthenticationContext(authority, TokenCache.DefaultShared);

var result = await context.AcquireTokenAsync(resource, credential);

return result.AccessToken;

}

}

}

On running the application, you will get the following output.

image

Summary

In this article we have explored how to create an Azure Key Vault & Access from C#.

Advantages of .Net Core, ASP.NET Core, EF Core

In this post I would like to List down the Advantages of .Net Core, ASP.NET Core, EF Core.

.Net Core Advantages

Open Source allowing source code availability & customization.

Cross-Platform runs on Windows, Linux and Mac OS.

Lightweight through newly written libraries, no dependency on windows OS libraries

High Performance in speed & efficiency

Scalability possible through Microservices & Containers supporting architecture

Disadvantages Third party library support are limited, Not available in Desktop applications.

ASP.NET Core Advantages

Additional to the open-source, cross-platform, light-weight advantages of .Net Core, following are the advantages of ASP.NET Core.

Unified story for building Web UI & Web APIs.

Testability friendly architecture through support for Interfaces.

Built-in Dependency Injection allowing singleton, scope, transient instance creation.

Host Friendly allowing hosting in IIS, Apache & Other web servers

Cloud-ready enabled for Azure, AWS hosting

EF Core Advantages

Additional to the open-source, cross-platform, light-weight advantages of .Net Core, following are the advantages of EF Core.

Batch Updates for sending multiple queries to the database thus reducing roundtrips & enhancing performance.

Alternate Keys support along with primary key.

In-Memory Provider for holding all entities in memory useful for unit testing.

Shadow Properties are those which are not in the entity class but tracked in the Change Tracker.

Mixing FromSQL with LINQ allowing SELECT * FROM statement mix with OrderBy() LINQ queries.

Note

All Core projects are complete re-write of the same.