Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Carlos Mendible
Carlos Mendible

Posted on • Originally published atcarlos.mendible.com on

     

Dapr: Reading local secrets with .NET 5

Now thatDapr is about to hit version 1.0.0 let me show you how easy is to read secrets with a.NET 5 console application.

Create a console application

dotnet new console -n DaprSecretSamplecd DaprSecretSample
Enter fullscreen modeExit fullscreen mode

Add a reference to the Dapr.Client library

dotnet add package Dapr.Client --prerelease
Enter fullscreen modeExit fullscreen mode

Create a Secret Store component

Create acomponents folder and inside place a file namedsecretstore.yaml with the following contents:

apiVersion: dapr.io/v1alpha1kind: Componentmetadata:  name: starwarssecretsspec:  type: secretstores.local.file  metadata:    - name: secretsFile      value: ./secrets.json
Enter fullscreen modeExit fullscreen mode

This component will enable Dapr, and therefore your application, to read secrets from asecrets.json file.

Create a Secrets file

Create asecrets.json file with the following contents:

{    "mandalorianSecret": "this is the way!"}
Enter fullscreen modeExit fullscreen mode

Replace the contents of Program.cs

Replace the contents ofProgram.cs with the following code:

using System;using Dapr.Client;var client = new DaprClientBuilder().Build();var secret = await client.GetSecretAsync("starwarssecrets", "mandalorianSecret");Console.WriteLine($"Secret from local file: {secret["mandalorianSecret"]});
Enter fullscreen modeExit fullscreen mode

Test the program

Run the following command to test the application.

dapr run --app-id secretapp --components-path .\components\ -- dotnet run
Enter fullscreen modeExit fullscreen mode

Experiment

One of the amazing things about Dapr is that you code will be the same even if you change the underlying secret store.

In your local environment you can also try reading “secrets” from environment variables. In order to do so, replace the contents of the./componentes/secrets.yaml file with:

apiVersion: dapr.io/v1alpha1kind: Componentmetadata:  name: starwarssecretsspec:  type: secretstores.local.env
Enter fullscreen modeExit fullscreen mode

Be sure to set, in your system, an environment variable namedmandalorianSecret, for instance:

export mandalorianSecret="May The Force be with you"
Enter fullscreen modeExit fullscreen mode

and run the application again:

dapr run --app-id secretapp --components-path .\components\ -- dotnet run
Enter fullscreen modeExit fullscreen mode

Note: I recommend using the secret stores shown in this post only for development or test scenarios. For a complete list of supported secret stores check the following repo:https://github.com/dapr/components-contrib/tree/master/secretstores.

Hope it helps!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Principal Cloud Solution Architect @Microsoft | Opinions are my own
  • Location
    Madrid. Spain
  • Work
    Principal Cloud Solution Architect @Microsoft
  • Joined

More fromCarlos Mendible

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp