Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AudiencesStore - create a ClientId(AudienceId) and Base64Secret(AudienceSecret) for JWT tokens
namespace WebApi.Identity.Providers
{
using System;
using System.Collections.Concurrent;
using System.Security.Cryptography;
using Microsoft.IdentityModel.Tokens;
public static class AudiencesStore
{
public static ConcurrentDictionary<string, Audience> AudiencesList = new ConcurrentDictionary<string, Audience>();
static AudiencesStore()
{
AudiencesList.TryAdd("6182376b3db44c399c77d5dfbd58df79", // GUID
new Audience
{
ClientId = "6182376b3db44c399c77d5dfbd58df79", // GUID
Base64Secret = "A1z9SxIfUM32I6YY_jbbe2m7S2eNMPVVxmUhRzmSC5I",
Name = "My.WebApi.Api 1"
});
}
public static Audience AddAudience(string name)
{
var clientId = Guid.NewGuid().ToString("N");
var key = new byte[32];
RNGCryptoServiceProvider.Create().GetBytes(key);
var base64Secret = Microsoft.IdentityModel.Tokens.Base64UrlEncoder.Encode(key);
Audience newAudience = new Audience { ClientId = clientId, Base64Secret = base64Secret, Name = name };
AudiencesList.TryAdd(clientId, newAudience);
return newAudience;
}
public static Audience FindAudience(string clientId)
{
Audience audience = null;
if (AudiencesList.TryGetValue(clientId, out audience))
{
return audience;
}
return null;
}
}
public class Audience
{
public string ClientId { get; set; }
public string Base64Secret { get; set; }
public string Name { get; set; }
public override string ToString()
{
return $"Name:{this.Name}, Base64Secret: {this.Base64Secret}, ClientId: {this.ClientId}";
}
}
}
class Program
{
static void Main(string[] args)
{
var webApi = AudiencesStore.AddAudience("My.WebApi API");
var mobileApp = AudiencesStore.AddAudience("My.WebApi Mobile App");
Console.WriteLine(webApi);
Console.WriteLine(mobileApp);
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment