Example #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, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
                {
                    HotModuleReplacement = true
                });
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            var options = new JwtBearerOptions
            {
                TokenValidationParameters =
                {
                    ValidIssuer              = JWTOptionsConst.ISSUER,
                    ValidAudience            = JWTOptionsConst.AUDIENCE,
                    IssuerSigningKey         = new SymmetricSecurityKey(JWTOptionsConst.GenerateKey()),
                    ValidateIssuerSigningKey = true,
                    ValidateLifetime         = true,
                }
            };


            app.UseStaticFiles();

            app.UseJwtBearerAuthentication(options);

            //app.UseIdentity();

            app.UseSession();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");

                routes.MapRoute(
                    name: "filterProduct",
                    template: "{controller=Home}/{action=Index}/{type?}");

                routes.MapRoute(
                    name: "searchProduct",
                    template: "{controller=Home}/{action=Index}/{keyword?}");

                routes.MapSpaFallbackRoute(
                    name: "spa-fallback",
                    defaults: new { controller = "Home", action = "Index" });
            });
        }
Example #2
0
        public static string GetToken(User user)
        {
            if (handler == null)
            {
                handler = new JwtSecurityTokenHandler();
            }

            var securityKey        = new SymmetricSecurityKey(JWTOptionsConst.GenerateKey());
            var signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);

            var identity = new ClaimsIdentity(new[] {
                new Claim(ClaimsTypeConst.ID, user._Id.ToString()),
                new Claim(ClaimsTypeConst.LOGIN, user.Login),
                new Claim(ClaimsIdentity.DefaultRoleClaimType, user.Roles[0])
            });

            var token = handler.CreateJwtSecurityToken(subject: identity,
                                                       signingCredentials: signingCredentials,
                                                       audience: JWTOptionsConst.AUDIENCE,
                                                       issuer: JWTOptionsConst.ISSUER,
                                                       expires: DateTime.UtcNow.AddMinutes(JWTOptionsConst.EXPIRES));

            return(handler.WriteToken(token));
        }