Created
January 22, 2018 08:25
-
-
Save kkoziarski/aec07024104f24ff88656bf372eb4503 to your computer and use it in GitHub Desktop.
AudiencesStore - create a ClientId(AudienceId) and Base64Secret(AudienceSecret) for JWT tokens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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