Beispiel #1
0
        /// <summary>
        /// Configure JWT. use in <seealso cref="Startup.Configure(IApplicationBuilder, Microsoft.AspNetCore.Hosting.IWebHostEnvironment)"/>
        /// </summary>
        /// <param name="self"></param>
        public static void ConfigureJWT(this IApplicationBuilder self)
        {
            self.UseAuthentication();

            using (var serviceScope = ServiceActivator.GetScope())
            {
                ILoggerFactory loggerFactory = serviceScope.ServiceProvider.GetService <ILoggerFactory>();
                IOptionsMonitor <JwtConfiguration> option = (IOptionsMonitor <JwtConfiguration>)serviceScope.ServiceProvider.GetService(typeof(IOptionsMonitor <JwtConfiguration>));


                ILogger logger = loggerFactory.CreateLogger(typeof(RegisterJWT));

                option.OnChange((o, s) =>
                {
                    if (original?.Issuer != option?.CurrentValue.Issuer || original?.Key != option?.CurrentValue.Key)
                    {
                        logger.LogWarning($"JwtConfiguration changed. Restart application to apply changes or discard them");
                    }
                    else
                    {
                        logger.LogInformation($"JwtConfiguration reset to original.");
                    }
                });
            }
        }
Beispiel #2
0
        /// <summary>
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            ServiceActivator.Configure(app.ApplicationServices);

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.ConfigureJWT();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            app.ConfigureSwagger();
        }