예제 #1
0
        static void Main(string[] args)
        {
            var nameId       = "*****@*****.**";
            var issuer       = "http://my.tokenissuer.com";
            var audience     = "http://my.website.com";
            var role         = "User";
            var subscription = Guid.NewGuid().ToString();

            var appConfig = new AppSettingsConfigReader();
            var kvUri     = appConfig["KeyVaultUri"];

            IConfigReader kvConfig   = new CredentialsKeyVaultConfigReader(appConfig);
            var           signingKey = kvConfig["JwtExample-SigningKey"];

            Console.WriteLine("Signing key: {0}", signingKey);

            var tokenData = JwtGen.GenerateToken(signingKey, nameId, audience, issuer, role, new Dictionary <string, string> {
                { nameof(subscription), subscription }
            });

            Console.WriteLine("Token data: {0}", tokenData);

            var token = JwtGen.ParseToken(tokenData);

            Console.WriteLine("Audience: {0}", token.Audiences.First());
            Console.WriteLine("Subscription: {0}", token.Claims.SingleOrDefault(c => c.Type == nameof(subscription)).Value);

            var principal = JwtGen.ValidateToken(tokenData, signingKey, new string[] { audience }, new string[] { issuer });

            Console.WriteLine("Principal: {0}", principal.Identity.Name);
            Console.WriteLine("Subscription: {0}", principal.Claims.SingleOrDefault(c => c.Type == nameof(subscription)).Value);

            Console.ReadLine();
        }
예제 #2
0
        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
            var configuration = builder.Build();

            IConfigReader appConfig     = new NetCoreSettingsConfigReader(configuration);
            IConfigReader credKVConfig1 = new CredentialsKeyVaultConfigReader(appConfig);
            IConfigReader envConfig     = new EnvVarsConfigReader(EnvironmentVariableTarget.Machine);
            IConfigReader credKVConfig2 = new CredentialsKeyVaultConfigReader(envConfig);

            var serviceProvider = new ServiceCollection()
                                  // .AddSingleton<IConfigReader>(provider => appConfig)
                                  // .AddSingleton<IConfigReader>(provider => envConfig)
                                  // .AddSingleton<IConfigReader>(provider => credKVConfig1)
                                  .AddSingleton <IConfigReader>(provider => credKVConfig2)
                                  .AddSingleton <IConsumer, ConfigConsumer>()
                                  .BuildServiceProvider();

            var consumer = serviceProvider.GetService <IConsumer>();

            consumer.Consume();
        }