Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Working with OpenShift secrets for ASP.NET Core

 

November 23, 2016
Takayoshi Tanaka
Related topics:
.NETKubernetes
Related products:
Red Hat OpenShift Container Platform

Share:

    If you want to use secret configuration which you don't want to store the code repository during developing ASP.NET Core app, what will you do? ASP.NET Core provides Secret Manager tool. Then how about developing on OpenShift? I'd like to talk about Secret Manager tool and working OpenShift secrets for ASP.NET Core in this blog.

    Secret Manager tool

    Let's try to use following the document. At first, make ASP.NET Core web project. Then add Microsoft.Extensions.SecretManager.Tools, Microsoft.Extensions.Configuration.UserSecrets and userSecretsId to the tools section of the project.json file. Be sure to make the userSecretsId value unique.

    "dependencies": {
      "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc2-final"
    },
    "userSecretsId": "aspnet-WebApp1-c23d27a4-eb88-4b18-9b77-2a93f3b15119",
    "tools": {
      "Microsoft.Extensions.SecretManager.Tools": "1.0.0-preview2-final"
    }
    $ dotnet user-secrets set MySecret ValueOfMySecret

    Once command is executed, the secrets.json file is generated at ~/.microsoft/usersecrets//secrets.json

    $ cat ~/.microsoft/usersecrets/aspnet-WebApp1-c23d27a4-eb88-4b18-9b77-2a93f3b15119/secrets.json
    {
      "MySecret": "ValueOfMySecret"
    }

    Then we can refer this secret value from code as below.

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
    
        if (env.IsDevelopment())
        {
            // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
            builder.AddUserSecrets();
        }
    
        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    Now it works storing secret data. However, this secret is stored in local machine folder. So how do we use secret on OpenShift?

    OpenShift secret

    OpenShift has a secret feature. To use OpenShift secret, we create yaml file as below. "strindData" field is a new feature at OpenShift v3.3. So if you use OpenShift 3.2 or lower, please use "data" field and value must be base64-encoded.

    apiVersion: "v1"
    kind: "Secret"
    metadata:
      name: "mysecret"
    stringData:
      mysecretconfig: '{"MySecret": "ValueOfMySecret"}'

    Then create secret with oc command.

    $ oc create -f mysecret.yaml

    Secret is mounted as a volume, so edit the deploymentconfig to mount the volume. If you haven't tried ASP.NET Core app on OpenSift, s2i-aspnet is a good starting point. In this example, secret data file located at /etc/secret-volume/mySecret.

    $ oc edit dc <your_dc>
    spec:
    <snip>
      containers:
        - name: myapp
          <snip>
          volumeMounts:
              # name must match the volume name below
              - name: secret-volume
                mountPath: /etc/secret-volume
                readOnly: true
      <snip>
      volumes:
        - name: secret-volume
          secret:
            secretName: mysecret
      restartPolicy: Never

    Or use oc command.

    $  oc volumes dc/<dc_name> --add --type=secret --secret-name=mysecret --mount-path=/etc/secret-volume

    To use this mounted secret file as a configuration, simply call Configuration.AddJsonFile method with file path.

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
    
        if (Directory.Exists("/etc/secret-volume"))
            builder.AddJsonFile("/etc/secret-volume/mysecretconfig", true);
    
        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
    
        Console.WriteLine(Configuration["MySecret"]);
    }

    Finally, you can refer the secret data from ASP.NET Core app on OpenShift. The full example project can be seen in my GitHub repo.

    For additional information and articles on .NET Core visit our .NET Core web page for more on this topic.

    Last updated: March 22, 2023

    Recent Posts

    • Run Qwen3-Next on vLLM with Red Hat AI: A step-by-step guide

    • How to implement observability with Python and Llama Stack

    • Deploy a lightweight AI model with AI Inference Server containerization

    • vLLM Semantic Router: Improving efficiency in AI reasoning

    • Declaratively assigning DNS records to virtual machines

    What’s up next?

     

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue