Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Products
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. CWE-798: Managing Secrets in ASP.NET Core with User Secrets and Azure Key Vault

CWE-798: Managing Secrets in ASP.NET Core with User Secrets and Azure Key Vault

Date- May 31,2026 246
cwe 798 aspnet core

Overview

The Common Weakness Enumeration (CWE) provides a catalog of software weaknesses that can lead to security vulnerabilities. CWE-798 specifically addresses the importance of managing sensitive information, such as API keys, connection strings, and other secrets, securely within applications. As applications scale and become more complex, the need to handle these secrets properly becomes paramount to prevent unauthorized access and data breaches.

Secrets management is critical for any application, especially those deployed in cloud environments where security is a top concern. Using tools like User Secrets and Azure Key Vault, developers can store and access sensitive information securely. User Secrets are ideal for local development, while Azure Key Vault provides a robust solution for production environments, ensuring that secrets are encrypted and access is controlled.

Prerequisites

  • ASP.NET Core: Familiarity with ASP.NET Core framework and its dependency injection system.
  • Visual Studio: An IDE for developing ASP.NET Core applications, preferably the latest version.
  • Azure Account: An active Azure subscription to utilize Azure Key Vault.
  • Command Line Interface: Basic understanding of using the command line for managing secrets.

User Secrets in ASP.NET Core

User Secrets allow developers to store sensitive information outside of the project tree in a JSON file, which is not included in source control. This is particularly useful during development, as it prevents sensitive data from being exposed in version control systems. User Secrets are tied to a specific user profile and can be accessed globally across different projects.

dotnet user-secrets init

The above command initializes User Secrets for your ASP.NET Core project. This creates a UserSecretsId in the project file, which allows the application to identify the secrets associated with it.

Once initialized, you can add secrets using the following command:

dotnet user-secrets set "MySecret" "SecretValue"

This command sets a secret named "MySecret" with a value of "SecretValue". Secrets are stored in a JSON file located in the user's profile directory.

Accessing User Secrets

To access User Secrets in your application, you need to configure the Startup.cs file. This is done by adding the User Secrets configuration provider to the configuration builder.

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
// Accessing a secret
var mySecret = Configuration["MySecret"];
// Use the secret as needed
}
}

This snippet demonstrates how to access the secret stored in User Secrets. The configuration object is injected into the Startup class, allowing access to the secret using its key.

Azure Key Vault

Azure Key Vault is a cloud service that provides a secure store for secrets, keys, and certificates. It is designed for production scenarios where security, compliance, and management are crucial. Azure Key Vault offers capabilities such as access policies, auditing, and integration with Azure Active Directory, making it a robust solution for secret management.

To use Azure Key Vault, you first need to create a Key Vault resource in the Azure portal. After creating it, you can add secrets through the portal or using Azure CLI commands.

az keyvault secret set --vault-name MyKeyVault --name MySecret --value "SecretValue"

This command sets a secret within the specified Key Vault. The secret can later be accessed programmatically from your ASP.NET Core application.

Integrating Azure Key Vault with ASP.NET Core

To integrate Azure Key Vault into your ASP.NET Core application, you need to install the required NuGet package:

dotnet add package Azure.Extensions.AspNetCore.Configuration.Secrets

Next, configure your application to use Azure Key Vault in the Startup.cs file:

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
var keyVaultEndpoint = new Uri("https://MyKeyVault.vault.azure.net/");
var client = new SecretClient(keyVaultEndpoint, new DefaultAzureCredential());
Configuration.AddAzureKeyVault(client, new KeyVaultSecretManager());
var mySecret = Configuration["MySecret"];
}
}

This code snippet initializes the Azure Key Vault client and adds it to the configuration system, allowing you to access secrets stored in the Key Vault. The DefaultAzureCredential automatically handles authentication for Azure resources.

Edge Cases & Gotchas

When managing secrets, there are several pitfalls to avoid. One common issue is forgetting to set the User Secrets or Azure Key Vault configuration correctly in the production environment. It’s crucial to ensure that secrets are not hardcoded into the application code, as this can lead to exposure and security vulnerabilities.

// Wrong approach - hardcoding secrets
var apiKey = "hardcoded_api_key";

In the example above, hardcoding secrets exposes sensitive information, making it easy for attackers to access it. Instead, always retrieve secrets from User Secrets or Azure Key Vault.

Performance & Best Practices

When using Azure Key Vault, consider the performance implications. Each request to retrieve a secret incurs network latency, so it’s advisable to cache secrets where feasible. Implementing a caching mechanism can significantly reduce the number of calls made to Azure Key Vault, improving application performance.

// Example of caching secrets
public class SecretService
{
private readonly IConfiguration _configuration;
private readonly IMemoryCache _cache;
public SecretService(IConfiguration configuration, IMemoryCache cache)
{
_configuration = configuration;
_cache = cache;
}

public string GetSecret(string key)
{
if (!_cache.TryGetValue(key, out string secret))
{
secret = _configuration[key];
_cache.Set(key, secret, TimeSpan.FromMinutes(5));
}
return secret;
}
}

This example demonstrates how to cache secrets for improved performance. The secrets are retrieved from the configuration and stored in memory for a defined duration.

Real-World Scenario: Mini-Project

To illustrate the concepts discussed, let’s create a mini-project that retrieves and displays a secret stored in Azure Key Vault. The project will include a simple ASP.NET Core web application that fetches a secret and displays it on a web page.

// Program.cs
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
// Startup.cs
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
var keyVaultEndpoint = new Uri("https://MyKeyVault.vault.azure.net/");
var client = new SecretClient(keyVaultEndpoint, new DefaultAzureCredential());
Configuration.AddAzureKeyVault(client, new KeyVaultSecretManager());
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
// HomeController.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

public class HomeController : Controller
{
private readonly IConfiguration _configuration;

public HomeController(IConfiguration configuration)
{
_configuration = configuration;
}

public IActionResult Index()
{
var mySecret = _configuration["MySecret"];
return View("Index", mySecret);
}
}
// Index.cshtml
@model string

Secret Value

@Model


This mini-project retrieves a secret from Azure Key Vault and displays it on a web page. The application consists of a simple controller that accesses the secret and passes it to the view, where it is rendered.

Conclusion

  • Understanding Secrets Management: Proper management of sensitive information is crucial for application security.
  • User Secrets: Ideal for local development, allowing sensitive data to be stored outside of source control.
  • Azure Key Vault: Provides a secure, scalable solution for managing secrets in production environments.
  • Best Practices: Implement caching and avoid hardcoding secrets to enhance performance and security.
  • Real-World Applications: Integrating secrets management into ASP.NET Core applications improves security posture significantly.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Securing ASP.NET Core appsettings.json Using Environment Variables and Secret Management
Jun 11, 2026
CWE-319: Enforcing HTTPS and HSTS in ASP.NET Core Applications
Apr 28, 2026
Implementing API Key Authentication Middleware in ASP.NET Core Web API
Jun 10, 2026
CWE-276: Fixing Insecure Default Configurations in ASP.NET Core Middleware Pipeline
Jun 09, 2026
Previous in ASP.NET Core
CWE-22: Preventing Path Traversal in ASP.NET Core File Handling
Next in ASP.NET Core
CWE-918: Preventing Server-Side Request Forgery (SSRF) in ASP.NET…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    CWE-269: Improper Privilege Management - Implementing the … 361 views
  • 2
    Send Email With HTML Template And PDF Using ASP.Net C# 17,203 views
  • 3
    Building Custom Bedrock Add-Ons with JavaScript: A Complet… 1,937 views
  • 4
    Error-An error occurred while processing your request in .… 11,964 views
  • 5
    Elasticsearch Integration in ASP.NET Core - Full-Text Sear… 243 views
  • 6
    Fix Gemini API Error 429: Quota Exceeded on Free Tier (Sys… 828 views
  • 7
    Integrating Google reCAPTCHA v3 in ASP.NET Core for Seamle… 613 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26683 views
  • Exception Handling Asp.Net Core 21720 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 21177 views
  • How to implement Paypal in Asp.Net Core 20127 views
  • Task Scheduler in Asp.Net core 18201 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1780
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Products
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor