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);
        }
        public static IDependencyResolver Configure(IdentityManagerConfiguration config)
        {
            if (config == null) throw new ArgumentNullException("config");

            var builder = new ContainerBuilder();
            builder
                .Register(ctx => config.UserManagerFactory())
                .As<IUserManager>()
                .InstancePerApiRequest();
            builder
                .RegisterApiControllers(typeof(AutofacConfig).Assembly);
            
            var container = builder.Build();
            return new AutofacWebApiDependencyResolver(container);
        }
        public static void Configure(HttpConfiguration apiConfig, IdentityManagerConfiguration idmConfig)
        {
            if (apiConfig == null) throw new ArgumentNullException("apiConfig");
            if (idmConfig == null) throw new ArgumentNullException("idmConfig");

            var resolver = AutofacConfig.Configure(idmConfig);
            apiConfig.DependencyResolver = resolver;

            apiConfig.MapHttpAttributeRoutes();
            if (!idmConfig.DisableUserInterface)
            {
                apiConfig.Routes.MapHttpRoute(Constants.RouteNames.Home,
                    "",
                    new { controller = "Page", action = "Index" });
                apiConfig.Routes.MapHttpRoute(Constants.RouteNames.OAuthFrameCallback,
                    "frame",
                    new { controller = "Page", action = "Frame" });
            }

            apiConfig.SuppressDefaultHostAuthentication();
            
            if (idmConfig.SecurityMode == SecurityMode.LocalMachine)
            {
                apiConfig.Filters.Add(new HostAuthenticationAttribute(Constants.LocalAuthenticationType));
            }
            else
            {
                apiConfig.Filters.Add(new HostAuthenticationAttribute(Constants.BearerAuthenticationType));
            }

            //apiConfig.Filters.Add(new AuthorizeAttribute() { Roles = idmConfig.AdminRoleName });
            apiConfig.Filters.Add(new AuthorizeAttribute());

            apiConfig.Formatters.Remove(apiConfig.Formatters.XmlFormatter);
            apiConfig.Formatters.Remove(apiConfig.Formatters.FormUrlEncodedFormatter);
            apiConfig.Formatters.JsonFormatter.SerializerSettings.ContractResolver =
                new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();

            //apiConfig.Services.Add(typeof(IExceptionLogger), new UserAdminExceptionLogger());

#if DEBUG
            apiConfig.Services.Add(typeof(IExceptionLogger), new TraceLogger());
#endif
        }
        public static void Configure(IAppBuilder app, IDependencyResolver resolver, IdentityManagerConfiguration config)
        {
            if (app == null) throw new ArgumentNullException("app");
            if (resolver == null) throw new ArgumentNullException("resolver");
            if (config == null) throw new ArgumentNullException("config");

            var apiConfig = new HttpConfiguration();
            apiConfig.MapHttpAttributeRoutes();
            apiConfig.DependencyResolver = resolver;

            apiConfig.SuppressDefaultHostAuthentication();
            apiConfig.Filters.Add(new HostAuthenticationAttribute("Bearer"));
            //apiConfig.Filters.Add(new AuthorizeAttribute(){Roles=config.AdminRoleName});

            apiConfig.Formatters.Remove(apiConfig.Formatters.XmlFormatter);
            apiConfig.Formatters.JsonFormatter.SerializerSettings.ContractResolver =
                new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();

            //apiConfig.Services.Add(typeof(IExceptionLogger), new UserAdminExceptionLogger());

            app.UseWebApi(apiConfig);
        }