Generate API Keys in C# (.NET)
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: C#
What: Generate cryptographically secure API keys in C# using <code>RandomNumberGenerator</code> for ASP.NET Core and .NET applications. Perfect for Web APIs and microservices.
Try it: Use our interactive Api-keys generator or integrate this code into your C# application.
Generate cryptographically secure API keys in C# using RandomNumberGenerator for ASP.NET Core and .NET applications. Perfect for Web APIs and microservices.
Looking for other languages? Check our code examples in
PHP
,
JavaScript
,
Python
,
Java
,
C++
,
Ruby
and
Go
or use our interactive web generator.
C# Code Example
using System;
using System.Security.Cryptography;
public class ApiKeyGenerator
{
public static string GenerateApiKey(int length = 32)
{
using var rng = RandomNumberGenerator.Create();
byte[] bytes = new byte[length];
rng.GetBytes(bytes);
// Convert to hexadecimal
return BitConverter.ToString(bytes).Replace("-", "").ToLower();
}
public static string GenerateApiKeyBase64(int length = 32)
{
using var rng = RandomNumberGenerator.Create();
byte[] bytes = new byte[length];
rng.GetBytes(bytes);
// Base64 URL-safe encoding
return Convert.ToBase64String(bytes)
.Replace('+', '-')
.Replace('/', '_')
.TrimEnd('=');
}
public static void Main()
{
// Generate 256-bit (32 byte) API key
string apiKey = GenerateApiKey(32);
Console.WriteLine($"Hex: {apiKey}");
// Output: "a7f3e9c2d8b4f1a6e5c9d7b3f8a2e6c4d9b5f7a3e8c6d2b9f4a7e1c5d8b3f6a9"
string apiKeyBase64 = GenerateApiKeyBase64(32);
Console.WriteLine($"Base64: {apiKeyBase64}");
// Output: "p3_pwr2L8RpuXJbT-IouaztX86jG0rmU9KfhxdizNpo"
}
}
[EXPLANATION]
C#'s RandomNumberGenerator class provides cryptographically secure random byte generation. The implementation properly disposes of the RNG with using statements. For .NET 6+, you can use RandomNumberGenerator.GetBytes() static method for cleaner code. The Base64 URL-safe encoding manually replaces special characters.
Expected Output
Hexadecimal: a7f3e9c2d8b4f1a6e5c9d7b3f8a2e6c4d9b5f7a3e8c6d2b9f4a7e1c5d8b3f6a9 Base64 URL-safe: p3_pwr2L8RpuXJbT-IouaztX86jG0rmU9KfhxdizNpo .NET 6+ static: kL7mP9tR3wX6hY2jN8vB4qS1eC5fA9xD0pT7gU6iM3n
Common Use Cases
- ASP.NET Core Web API authentication
- Azure Function app keys
- Blazor server authentication
- .NET microservices communication
- Windows service authentication
Important Notes
-
Use
RandomNumberGenerator, notRandom -
.NET 6+: use
RandomNumberGenerator.GetBytes()static method -
Always dispose with
usingstatements - Consider Guid.NewGuid() for shorter 128-bit keys
- Hash API keys with BCrypt.NET before storage
Try Our Interactive Generator
Don't want to write code? Use our free web-based Api-keys generator with instant results.
TRY API-KEYS GENERATOR →Other Programming Languages
View Api-keys generation code examples in PHP
View Api-keys generation code examples in JavaScript
View Api-keys generation code examples in Python
View Api-keys generation code examples in Java
View Api-keys generation code examples in C++
View Api-keys generation code examples in Ruby
View Api-keys generation code examples in Go