public TokenProviderMiddleware(
     RequestDelegate next,
     IOptions<TokenProviderOptions> options,
     AppDbContext context)
 {
     _next = next;
     _options = options.Value;
     _context = context;
 }
Ejemplo 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, ILoggerFactory loggerFactory, IOptions<TokenProviderOptions> tokenOptions, AppDbContext dbContext)
        {
            if (env.IsProduction())
            {
                dbContext.Database.Migrate();
            }

            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.Use(async (context, next) =>
            {
                await next();

                if (context.Response.StatusCode == 404 &&
                    !Path.HasExtension(context.Request.Path.Value) &&
                    !context.Request.Path.Value.StartsWith("/api/"))
                {
                    context.Request.Path = "/index.html";
                    await next();
                }
            });

            var jwtOptions = tokenOptions.Value;

            app.UseJwtBearerAuthentication(new JwtBearerOptions
            {
                AutomaticAuthenticate = true,
                AutomaticChallenge = true,
                TokenValidationParameters = jwtOptions.TokenValidationParameters
            });

            app.UseMiddleware<TokenProviderMiddleware>();

            app.UseMvc();
            app.UseStaticFiles();
        }