Exemple #1
0
        public static bool TokenStatus(string token)
        {
            Chilkat.Jwt jwt = new Chilkat.Jwt();
            //Chilkat.Jwt jwt = new Chilkat.Jwt();
            //string payload = jwt.GetPayload(token);
            //Console.WriteLine(payload);


            Chilkat.PublicKey pubKey = new Chilkat.PublicKey();
            bool success             = pubKey.LoadFromFile("C:\\Users\\Lenovo\\Desktop\\Detyra1_DS-Gr-6-master\\ds\\bin\\Debug\\netcoreapp3.0\\keys\\" +
                                                           GetTuser(token, false) + ".pub.pem");

            Console.WriteLine(GetTuser(token, false));
            bool sigVerified = jwt.VerifyJwtPk(token, pubKey);

            if (sigVerified)
            {
                // Console.WriteLine("valid " + Convert.ToString(sigVerified));
                int  leeway     = 60;
                bool bTimeValid = jwt.IsTimeValid(token, leeway);
                Console.WriteLine("time constraints valid: " + Convert.ToString(bTimeValid));
                if (bTimeValid)
                {
                    // Console.WriteLine(GetTuser(token, true));
                }
                return(true);
            }
            else
            {
                // Console.WriteLine("nuk eshte valid");
                return(false);
            }
        }
        // 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();
        }