Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            //
            // Build Config
            var            configHelper = new ConfigurationHelper(args);
            IConfiguration config       = configHelper.Build();

            //
            // Initialize runAsAService local variable
            string serviceName   = config.GetValue <string>("serviceName")?.Trim();
            bool   runAsAService = !string.IsNullOrEmpty(serviceName);

            //
            // Host
            using (var host = new WebHostBuilder()
                              .UseContentRoot(configHelper.RootPath)
                              .ConfigureLogging((hostingContext, logging) => {
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));

                //
                // Console log is not available in running as a Service
                if (!runAsAService)
                {
                    logging.AddConsole();
                }

                logging.AddDebug();
                logging.AddEventLog(new EventLogSettings()
                {
                    SourceName = EventSourceName
                });
            })
                              .UseUrls("https://*:55539")                     // Config can override it. Use "urls":"https://*:55539"
                              .UseConfiguration(config)
                              .ConfigureServices(s => s.AddSingleton(config)) // Configuration Service
                              .UseStartup <Startup>()
                              .UseHttpSys(o => {
                //
                // Kernel mode Windows Authentication
                o.Authentication.Schemes = AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM;

                //
                // Need anonymous to allow CORS preflight requests
                // app.UseWindowsAuthentication ensures (if needed) the request is authenticated to proceed
                o.Authentication.AllowAnonymous = true;
            })
                              .Build()
                              .UseHttps()) {
                if (runAsAService)
                {
                    //
                    // Run as a Service
                    Log.Information($"Running as service: {serviceName}");
                    host.RunAsService();
                }
                else
                {
                    //
                    // Run interactive
                    host.Run();
                }
            }
        }
Ejemplo n.º 2
0
        // Configure is called after ConfigureServices is called.
        public void Configure(IApplicationBuilder app,
                              IHttpContextAccessor contextAccessor)
        {
            //
            // Initialize the Environment
            //
            Core.Environment.Host = AdminHost.Instance;
            Core.Environment.Hal  = new HalService();
            AdminHost.Instance.Add(this);

            // Context accessor
            HttpHelper.HttpContextAccessor = contextAccessor;

            // Initalize Config
            ConfigurationHelper.Initialize(HostingEnvironment.GetConfigPath("appsettings.json"));


            //
            // Error handling
            //
            app.UseErrorHandler();

            //
            // Ensure SSL
            //
            app.UseMiddleware <SSLCheck>();

            //
            // Static files
            //
            app.UseStaticFiles();


            //
            // CORS
            //
            app.UseCrossOrigin("/" + Globals.API_PATH);


            //
            // Authentication
            //
            app.UseBearerAuthentication();


            //
            // Authorization
            //
            app.UseUrlAuthorization(new UrlAuthorizatonOptions
            {
                Path = "/" + Globals.API_PATH,  // /api
                AuthenticationScheme = JwtBearerDefaults.AuthenticationScheme,
                PolicyName           = "AccessToken"
            });

            app.UseUrlAuthorization(new UrlAuthorizatonOptions
            {
                Path = "/" + Globals.SECURITY_PATH, // /security
                AuthenticationScheme = "NTLM",
                PolicyName           = "AdministrativeGroup"
            });


            //
            // Disable client cache
            //
            app.Use(async(context, next) =>
            {
                context.Response.Headers[Net.Http.Headers.HeaderNames.CacheControl] = "public, max-age=0";
                await next.Invoke();
            });


            //
            // Allow HEAD requests as GET
            app.UseMiddleware <HeadTransform>();



            //
            // Add MVC
            //
            app.UseMvc(routes => {
                AdminHost.Instance.StartModules(routes, app);
                InitiateFeatures(routes);

                // Ensure routes meant to be extended do not block child routes
                SortRoutes(routes);
            });
        }