Example #1
0
        // This method is responsible for generating public and private key if keys are not present in consul
        public static async System.Threading.Tasks.Task KeyGeneratorAsync(ConsulClient client)
        {
            Chilkat.Global glob = new Chilkat.Global();
            glob.UnlockBundle("Anything for 30-day trial");

            Chilkat.Rsa rsaKey = new Chilkat.Rsa();

            rsaKey.GenerateKey(1024);
            var rsaPrivKey         = rsaKey.ExportPrivateKeyObj();
            var rsaPrivKeyAsString = rsaKey.ExportPrivateKey();

            var rsaPublicKey         = rsaKey.ExportPublicKeyObj();
            var rsaPublicKeyAsString = rsaKey.ExportPublicKey();

            var putPair = new KVPair("myPublicKey")
            {
                Value = Encoding.UTF8.GetBytes(rsaPublicKeyAsString)
            };
            var putAttempt = await client.KV.Put(putPair);


            var putPair1 = new KVPair("myPrivateKey")
            {
                Value = Encoding.UTF8.GetBytes(rsaPrivKeyAsString)
            };
            var putAttempt1 = await client.KV.Put(putPair1);
        }
Example #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors(x => x
                        .AllowAnyOrigin()
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials()
                        );


            app.Use(async(context, next) =>
            {
                Console.WriteLine(context.Request.Path.Value);
                if (context.Request.Path.Value == "/" || context.Request.Path.Value.Contains("/assets") || context.Request.Path.Value.StartsWith("/auth") || context.Request.Path.Value.Contains("/signIn") || context.Request.Path.Value.Contains("/signUp") || context.Request.Path.Value.Contains("/socialSignIn"))
                {
                    await next();
                }
                else
                {
                    Microsoft.AspNetCore.Http.IRequestCookieCollection cookies = context.Request.Cookies;
                    var token = cookies["TOKEN"];
                    Console.WriteLine(token);
                    // var token = context.Request.Cookies["TOKEN"] ;
                    // var token = context.Request.Headers["Authorization"];
                    Chilkat.Global glob = new Chilkat.Global();
                    glob.UnlockBundle("Anything for 30-day trial");

                    using (var client = new ConsulClient())
                    {
                        string ConsulIpHost   = "http://consul:8500";
                        client.Config.Address = new Uri(ConsulIpHost);
                        // client.Config.Address = new Uri("http://172.23.238.173:8500");
                        var getpair2  = client.KV.Get("myPublicKey");
                        string secret = System.Text.Encoding.UTF8.GetString(getpair2.Result.Response.Value);
                        Chilkat.Rsa rsaExportedPublicKey = new Chilkat.Rsa();
                        rsaExportedPublicKey.ImportPublicKey(secret);
                        var publickey = rsaExportedPublicKey.ExportPublicKeyObj();
                        Console.WriteLine(rsaExportedPublicKey.ExportPublicKey());
                        var jwt = new Chilkat.Jwt();
                        if (jwt.VerifyJwtPk(token, publickey) && jwt.IsTimeValid(token, 0))
                        {
                            await next();
                        }
                        else
                        {
                            context.Response.StatusCode = 403;
                            await context.Response.WriteAsync("UnAuthorized");
                        }
                    }
                }
            });
            app.UseWebSockets();
            app.UseOcelot().Wait();
        }
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            Chilkat.Global glob = new Chilkat.Global();
            glob.UnlockBundle("Anything for 30-day trial");

            Chilkat.Rsa rsaKey = new Chilkat.Rsa();

            rsaKey.GenerateKey(1024);
            var rsaPrivKey = rsaKey.ExportPrivateKeyObj();

            var rsaPublicKey         = rsaKey.ExportPublicKeyObj();
            var rsaPublicKeyAsString = rsaKey.ExportPublicKey();

            Chilkat.JsonObject jwtHeader = new Chilkat.JsonObject();
            jwtHeader.AppendString("alg", "RS256");
            jwtHeader.AppendString("typ", "JWT");

            Chilkat.JsonObject claims = new Chilkat.JsonObject();
            claims.AppendString("Email", "*****@*****.**");
            claims.AppendString("Test", "test1");

            Chilkat.Jwt jwt = new Chilkat.Jwt();

            string token = jwt.CreateJwtPk(jwtHeader.Emit(), claims.Emit(), rsaPrivKey);

            Console.WriteLine("This is the token generated");
            Console.WriteLine(token);

            // Verifying Token using Public Key
            Console.WriteLine(jwt.VerifyJwtPk(token, rsaPublicKey));
            Console.WriteLine(jwt.GetPayload(token));

            // Importing public key
            Chilkat.Rsa rsaExportedPublicKey = new Chilkat.Rsa();
            Console.WriteLine(rsaExportedPublicKey.ImportPublicKey(rsaPublicKeyAsString));
            Console.WriteLine(jwt.VerifyJwtPk(token, rsaExportedPublicKey.ExportPublicKeyObj()));

            // Store the value in Consul KV
            using (var client = new ConsulClient())
            {
                var putPair = new KVPair("secretkey")
                {
                    Value = Encoding.UTF8.GetBytes(rsaPublicKeyAsString)
                };

                var putAttempt = await client.KV.Put(putPair);

                if (putAttempt.Response)
                {
                    var getPair = await client.KV.Get("secretkey");

                    if (getPair.Response != null)
                    {
                        Console.WriteLine("Getting Back the Stored String");
                        Console.WriteLine(Encoding.UTF8.GetString(getPair.Response.Value, 0, getPair.Response.Value.Length));
                    }
                }
            }
        }