public static void UseIdentityManager(this IAppBuilder app, IdentityManagerConfiguration config)
        {
            if (app == null) throw new ArgumentNullException("app");
            if (config == null) throw new ArgumentNullException("config");
            config.Validate();

            app.Use(async (ctx, next) =>
            {
                var localAddresses = new string[]{"127.0.0.1", "::1", ctx.Request.LocalIpAddress};
                if (localAddresses.Contains(ctx.Request.RemoteIpAddress))
                {
                    await next();
                }
            });

            app.UseFileServer(new FileServerOptions
            {
                RequestPath = new PathString("/assets"),
                FileSystem = new EmbeddedResourceFileSystem(typeof(AppBuilderExtensions).Assembly, "Thinktecture.IdentityManager.Core.Assets")
            });
            app.UseFileServer(new FileServerOptions
            {
                RequestPath = new PathString("/assets/libs/fonts"),
                FileSystem = new EmbeddedResourceFileSystem(typeof(AppBuilderExtensions).Assembly, "Thinktecture.IdentityManager.Core.Assets.Content.fonts")
            });
            app.UseStageMarker(PipelineStage.MapHandler);

            //app.UseJsonWebToken();
            var resolver = AutofacConfig.Configure(config);
            WebApiConfig.Configure(app, resolver, config);
        }
        public static void UseIdentityManager(this IAppBuilder app, IdentityManagerConfiguration config)
        {
            if (app == null) throw new ArgumentNullException("app");
            if (config == null) throw new ArgumentNullException("config");
            config.Validate();

            if (config.SecurityMode == SecurityMode.LocalMachine)
            {
                var local = new LocalAuthenticationOptions(config.AdminRoleName);
                app.Use<LocalAuthenticationMiddleware>(local);
            }
            else if (config.SecurityMode == SecurityMode.OAuth2)
            {
                if (config.OAuth2Configuration.SigningCert != null)
                {
                    app.UseJsonWebToken(config.OAuth2Configuration.Issuer,
                        config.OAuth2Configuration.Audience,
                        config.OAuth2Configuration.SigningCert);
                }
                else
                {
                    app.UseJsonWebToken(config.OAuth2Configuration.Issuer,
                        config.OAuth2Configuration.Audience,
                        config.OAuth2Configuration.SigningKey);
                }
                app.Use(async(ctx, next) =>
                {
                    await next();
                });
            }

            if (!config.DisableUserInterface)
            {
                app.UseFileServer(new FileServerOptions
                {
                    RequestPath = new PathString("/assets"),
                    FileSystem = new EmbeddedResourceFileSystem(typeof(AppBuilderExtensions).Assembly, "Thinktecture.IdentityManager.Assets")
                });
                app.UseFileServer(new FileServerOptions
                {
                    RequestPath = new PathString("/assets/libs/fonts"),
                    FileSystem = new EmbeddedResourceFileSystem(typeof(AppBuilderExtensions).Assembly, "Thinktecture.IdentityManager.Assets.Content.fonts")
                });
                app.UseStageMarker(PipelineStage.MapHandler);
            }

            SignatureConversions.AddConversions(app);

            var httpConfig = new HttpConfiguration();
            WebApiConfig.Configure(httpConfig, config);
            app.UseWebApi(httpConfig);
            app.UseStageMarker(PipelineStage.MapHandler);
        }