Esempio n. 1
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();
        }
Esempio n. 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(builder => builder
                        .AllowAnyOrigin()
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials()
                        ); //for CORS

            app.Use(async(context, next) => {
                //var token = context.Request.Headers["Authorization"];
                // var token = context.Request.Cookies["UserLoginAPItoken"];

                //switch(context.Request.Path.ToString())
                Console.WriteLine(context.Request.Path.ToString());
                //switch(context.Request.Path.ToString())
                if (context.Request.Path.Value.StartsWith("/auth") || context.Request.Path.Value.StartsWith("/gameplay") || context.Request.Path.Value.StartsWith("/favicon") ||
                    context.Request.Path.Value.StartsWith("/questiongenerator") || context.Request.Path.Value.StartsWith("/quizmaster"))
                {
                    Console.WriteLine("Calling next middleware");
                    await next();
                }
                else
                {
                    Microsoft.AspNetCore.Http.IRequestCookieCollection cookies = context.Request.Cookies;
                    var token           = cookies["UserLoginAPItoken"];
                    Chilkat.Global glob = new Chilkat.Global();
                    glob.UnlockBundle("Anything for 30-day trial");

                    using (var client = new ConsulClient())
                    {
                        Console.WriteLine("---------entered consul----------------");
                        client.Config.Address = new Uri("http://consul:8500");
                        var getpair2          = client.KV.Get("secretkey");
                        Console.WriteLine(getpair2);
                        Console.WriteLine("------got the getpair2------");
                        Console.WriteLine("-------key-----" + getpair2.Result.Response.Key);
                        Console.WriteLine("------Value-----" + getpair2.Result.Response.Value);
                        //var getresult = getpair2.Result.Response.Value
                        // if(getpair2.Result.Response.Value != null)
                        // {
                        Console.WriteLine("---------Entered the function");
                        string secret = System.Text.Encoding.UTF8.GetString(getpair2.Result.Response.Value);
                        Console.WriteLine("------------Secret Key------------" + secret);
                        Chilkat.Rsa rsaExportedPublicKey = new Chilkat.Rsa();
                        rsaExportedPublicKey.ImportPublicKey(secret);
                        var publickey = rsaExportedPublicKey.ExportPublicKeyObj();
                        Console.WriteLine("--------publickey--------" + publickey);
                        Console.WriteLine("-----token-----" + token);
                        var jwt = new Chilkat.Jwt();
                        if (jwt.VerifyJwtPk(token, publickey))
                        {
                            Console.WriteLine("--inside verify");
                            await next();
                        }
                        else
                        {
                            context.Response.StatusCode = 403;
                            await context.Response.WriteAsync("UnAuthorized");
                        }
                    }
                }
            });

            app.UseWebSockets();
            app.UseOcelot().Wait();
        }