C#

Fetch secrets for your C# ASP.NET Core application using the Doppler API.

We're still building out first-class ASP.NET Core support and if you'd like to be notified when it's ready for technical preview, please reach out to us at [email protected].

In the meantime, you can use the below code sample as a starting point which uses our download secrets API for fetching secrets at runtime.

Service Token

The first step is to create a Service Token to provide read-only access to the project and config you wish to supply secrets for:

Then expose the Service Token value to your environment using the DOPPLER_TOKEN environment variable

export DOPPLER_TOKEN='dp.st.dev.xxxx'
$env:DOPPLER_TOKEN = 'dp.st.dev.xxxx'
SET VAR_NAME=dp.st.dev.xxxx

Secrets API

Here is a simple secrets fetching example.

πŸ“˜

Let us know if you think this sample code can be improved by emailing us at [email protected].

using System;
using System.Text;
using Newtonsoft.Json;
using RestSharp;

namespace Doppler.Examples
{
    public class Doppler
    {
        [JsonProperty("DOPPLER_PROJECT")]
        public string DopplerProject { get; set; }

        [JsonProperty("DOPPLER_ENVIRONMENT")]
        public string DopplerEnvironment { get; set; }

        [JsonProperty("DOPPLER_CONFIG")]
        public string DopplerConfig { get; set; }

        public static Doppler FetchSecrets()
        {
            var DOPPLER_TOKEN = Environment.GetEnvironmentVariable("DOPPLER_TOKEN");
            var client = new RestClient("https://api.doppler.com/v3/configs/config/secrets/download?format=json");
            var request = new RestRequest(Method.GET);
            request.AddHeader("Authorization", $"Basic {Convert.ToBase64String(Encoding.Default.GetBytes(DOPPLER_TOKEN + ":"))}");
            IRestResponse response = client.Execute(request);
            return JsonConvert.DeserializeObject<Doppler>(response.Content);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var secrets = Doppler.FetchSecrets();
            Console.WriteLine($"Project: {secrets.DopplerProject}");
            Console.WriteLine($"Environment: {secrets.DopplerEnvironment}");
            Console.WriteLine($"Config: {secrets.DopplerConfig}");
        }
    }
}

πŸ‘

Awesome Work!

Now you know how to fetch secrets for your ASP.NET Core C# application using the secrets download API.


Did this page help you?