コード例 #1
0
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory, IServiceProvider serviceProvider)
        {
            app.UseStatusCodePagesWithReExecute("/Home/Status/{0}");

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");

                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection().UseWwwRedirection();
            app.UseStaticFiles(new StaticFileOptions()
            {
                OnPrepareResponse = (context) =>
                {
                    Microsoft.AspNetCore.Http.Headers.ResponseHeaders headers = context.Context.Response.GetTypedHeaders();
                    headers.CacheControl = new CacheControlHeaderValue()
                    {
                        MaxAge = TimeSpan.FromDays(3),
                    };
                }
            });

            app.UseCookiePolicy();
            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                // map routes with area and no controller
                endpoints.MapControllerRoute(name: "AreaRouteNoController",
                                             pattern: "{area:exists}/{action}",
                                             defaults: new { controller = "Home", action = "Index" });

                // map routes with area
                endpoints.MapControllerRoute(name: "AreaRoute",
                                             pattern: "{area:exists}/{controller}/{action}",
                                             defaults: new { controller = "Home", action = "Index" });

                // map routes with no controller
                endpoints.MapControllerRoute(name: "RouteNoController",
                                             pattern: "{action}",
                                             new { controller = "Home", action = "Index" });

                // default
                endpoints.MapControllerRoute(name: "default",
                                             pattern: "{controller}/{action}/{id?}",
                                             new { controller = "Home", action = "Index" });

                endpoints.MapRazorPages();
            });
        }
コード例 #2
0
ファイル: Startup.cs プロジェクト: afrog33k/altinn-studio
        /// <summary>
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="appBuilder">The application builder</param>
        /// <param name="env">The hosting environment</param>
        public void Configure(IApplicationBuilder appBuilder, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                appBuilder.UseDeveloperExceptionPage();
            }
            else
            {
                appBuilder.UseExceptionHandler("/Error");
            }

            // appBuilder.UseHsts();
            // appBuilder.UseHttpsRedirection();
            appBuilder.UseAuthentication();

            appBuilder.UseStatusCodePages(async context =>
            {
                var request  = context.HttpContext.Request;
                var response = context.HttpContext.Response;
                string url   = $"https://{request.Host.ToString()}{request.Path.ToString()}";

                // you may also check requests path to do this only for specific methods
                // && request.Path.Value.StartsWith("/specificPath")
                if (response.StatusCode == (int)HttpStatusCode.Unauthorized)
                {
                    response.Redirect($"account/login?gotoUrl={url}");
                }
            });

            appBuilder.UseResponseCompression();
            appBuilder.UseRequestLocalization();
            appBuilder.UseStaticFiles(new StaticFileOptions()
            {
                OnPrepareResponse = (context) =>
                {
                    Microsoft.AspNetCore.Http.Headers.ResponseHeaders headers = context.Context.Response.GetTypedHeaders();
                    headers.CacheControl = new CacheControlHeaderValue()
                    {
                        Public = true,
                        MaxAge = TimeSpan.FromMinutes(60),
                    };
                },
            });

            appBuilder.UseMvc(routes =>
            {
                // ---------------------------- UI --------------------------- //
                routes.MapRoute(
                    name: "profileApiRoute",
                    template: "{org}/{app}/api/v1/{controller}/user/",
                    defaults: new
                {
                    action     = "GetUser",
                    controller = "Profile"
                },
                    constraints: new
                {
                    action     = "GetUser",
                    controller = "Profile",
                });
                routes.MapRoute(
                    name: "uiRoute",
                    template: "{org}/{app}/{partyId}/{instanceGuid}/{action}/{view|validation?}/{itemId?}",
                    defaults: new { controller = "Instance" },
                    constraints: new
                {
                    action       = "CompleteAndSendIn|Lookup|ModelValidation|Receipt|StartService|ViewPrint|edit",
                    controller   = "Instance",
                    app          = "[a-zA-Z][a-zA-Z0-9_\\-]{2,30}",
                    instanceGuid = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$",
                });

                routes.MapRoute(
                    name: "uiEditRoute",
                    template: "{org}/{app}/{instanceId?}",
                    defaults: new { action = "EditSPA", controller = "Instance" },
                    constraints: new
                {
                    action     = "EditSPA",
                    controller = "Instance",
                    app        = "[a-zA-Z][a-zA-Z0-9_\\-]{2,30}",
                    instanceId = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$",
                });

                routes.MapRoute(
                    name: "runtimeRoute",
                    template: "{org}/{app}",
                    defaults: new { action = "EditSPA", controller = "Instance" },
                    constraints: new
                {
                    action     = "EditSPA",
                    controller = "Instance",
                    app        = "[a-zA-Z][a-zA-Z0-9_\\-]{2,30}",
                });

                routes.MapRoute(
                    name: "instantiateRoute",
                    template: "{org}/{app}/{controller}/InstantiateApp",
                    defaults: new { action = "InstantiateApp", controller = "Instance" },
                    constraints: new
                {
                    action     = "InstantiateApp",
                    controller = "Instance",
                    app        = "[a-zA-Z][a-zA-Z0-9_\\-]{2,30}",
                });

                routes.MapRoute(
                    name: "authentication",
                    template: "{org}/{app}/{controller}/{action}/{goToUrl?}",
                    defaults: new { action = "Login", controller = "Account" },
                    constraints: new
                {
                    action     = "Login",
                    controller = "Account",
                    app        = "[a-zA-Z][a-zA-Z0-9_\\-]{2,30}",
                });

                // ---------------------------- API -------------------------- //
                routes.MapRoute(
                    name: "resourceRoute",
                    template: "{org}/{app}/api/resource/{id}",
                    defaults: new { action = "Index", controller = "Resource" },
                    constraints: new
                {
                    controller = "Resource",
                    app        = "[a-zA-Z][a-zA-Z0-9_\\-]{2,30}",
                });

                routes.MapRoute(
                    name: "textresourceRoute",
                    template: "{org}/{app}/api/textresources",
                    defaults: new { action = "TextResources", controller = "Resource" },
                    constraints: new
                {
                    controller = "Resource",
                    app        = "[a-zA-Z][a-zA-Z0-9_\\-]{2,30}",
                });

                routes.MapRoute(
                    name: "runtimeResourceRoute",
                    template: "{org}/{app}/api/runtimeresources/{id}/",
                    defaults: new { action = "RuntimeResource", controller = "Resource" },
                    constraints: new
                {
                    controller = "Resource",
                });

                routes.MapRoute(
                    name: "metadataRoute",
                    template: "{org}/{app}/api/metadata/{action=Index}",
                    defaults: new { controller = "Resource" },
                    constraints: new
                {
                    controller = "Resource",
                    app        = "[a-zA-Z][a-zA-Z0-9_\\-]{2,30}",
                });

                routes.MapRoute(
                    name: "apiPostRoute",
                    template: "{org}/{app}/api/{reportee}/{apiMode}",
                    defaults: new { action = "Index", controller = "ServiceAPI" },
                    constraints: new
                {
                    controller = "ServiceAPI",
                    app        = "[a-zA-Z][a-zA-Z0-9_\\-]{2,30}",
                });

                routes.MapRoute(
                    name: "apiAttachmentRoute",
                    template: "{org}/{app}/api/attachment/{partyId}/{instanceGuid}/{action}",
                    defaults: new { controller = "Instance" },
                    constraints: new
                {
                    controller   = "Instance",
                    app          = "[a-zA-Z][a-zA-Z0-9_\\-]{2,30}",
                    instanceGuid = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$",
                });

                routes.MapRoute(
                    name: "apiPutRoute",
                    template: "{org}/{app}/api/{reportee}/{instanceId}/{apiMode}",
                    defaults: new { action = "Index", controller = "ServiceAPI" },
                    constraints: new
                {
                    controller = "ServiceAPI",
                    app        = "[a-zA-Z][a-zA-Z0-9_\\-]{2,30}",
                    instanceId = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$",
                });
                routes.MapRoute(
                    name: "apiWorkflowRoute",
                    template: "{org}/{app}/api/workflow/{partyId}/{instanceId}/{action=GetCurrentState}",
                    defaults: new { controller = "ServiceAPI" },
                    constraints: new
                {
                    controller = "ServiceAPI",
                    partyId    = "[0-9]+",
                    app        = "[a-zA-Z][a-zA-Z0-9_\\-]{2,30}",
                    instanceId = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$",
                });

                routes.MapRoute(
                    name: "codelistRoute",
                    template: "{org}/{app}/api/{controller}/{action=Index}/{name}",
                    defaults: new { controller = "Codelist" },
                    constraints: new
                {
                    controller = "Codelist",
                    app        = "[a-zA-Z][a-zA-Z0-9_\\-]{2,30}",
                });

                routes.MapRoute(
                    name: "apiRoute",
                    template: "{org}/{app}/api/{partyId}/{instanceId}",
                    defaults: new { action = "Gindex", controller = "ServiceAPI" },
                    constraints: new
                {
                    controller = "ServiceAPI",
                    app        = "[a-zA-Z][a-zA-Z0-9_\\-]{2,30}",
                    partyId    = "[0-9]{1,20}",
                    instanceId = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$",
                });

                routes.MapRoute(
                    name: "serviceRoute",
                    template: "{org}/{app}/{controller}/{action=Index}/{id?}",
                    defaults: new { controller = "Service" },
                    constraints: new
                {
                    controller = @"(Codelist|Config|Model|Rules|ServiceMetadata|Text|UI|Workflow|React)",
                    app        = "[a-zA-Z][a-zA-Z0-9_\\-]{2,30}",
                    id         = "[a-zA-Z0-9_\\-]{1,30}",
                });
                routes.MapRoute(
                    name: "languageRoute",
                    template: "{org}/{app}/api/{controller}/{action=Index}/{id?}",
                    defaults: new { controller = "Language" },
                    constraints: new
                {
                    controller = "Language",
                });

                routes.MapRoute(
                    name: "authorization",
                    template: "{org}/{app}/api/{controller}/parties/{partyId}/validate",
                    defaults: new { action = "ValidateSelectedParty", controller = "Authorization" },
                    constraints: new
                {
                    action     = "ValidateSelectedParty",
                    controller = "Authorization",
                    app        = "[a-zA-Z][a-zA-Z0-9_\\-]{2,30}",
                });

                /* routes.MapRoute(
                 *   name: "authenticationRoute",
                 *   template: "{controller}/{action}/{gotourl?}",
                 *   defaults: new { controller = "Account" },
                 *   constraints: new
                 *   {
                 *       controller = "Account",
                 *   }); */

                // -------------------------- DEFAULT ------------------------- //
                routes.MapRoute(
                    name: "defaultRoute2",
                    template: "{controller}/{action=Index}/{id?}");

                routes.MapRoute(
                    name: "defaultRoute",
                    template: "{action=Index}/{id?}");
            });

            appBuilder.UseSwagger();

            appBuilder.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Altinn Apps Runtime API");
            });
        }
コード例 #3
0
        public BundlingMiddleware(RequestDelegate next, IWebHostEnvironment env, ILoggerFactory loggerFactory, IHttpContextAccessor httpContextAccessor,
                                  IOptions <BundleGlobalOptions> globalOptions, IBundleManagerFactory bundleManagerFactory, BundleCollection bundles, IOptions <BundlingOptions> options)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }

            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            if (httpContextAccessor == null)
            {
                throw new ArgumentNullException(nameof(httpContextAccessor));
            }

            if (globalOptions == null)
            {
                throw new ArgumentNullException(nameof(globalOptions));
            }

            if (bundleManagerFactory == null)
            {
                throw new ArgumentNullException(nameof(bundleManagerFactory));
            }

            if (bundles == null)
            {
                throw new ArgumentNullException(nameof(bundles));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _next = next;

            BundlingOptions optionsUnwrapped = options.Value;

            _bundleManager = optionsUnwrapped.BundleManager ?? bundleManagerFactory.Create(bundles, new BundlingContext
            {
                BundlesPathPrefix     = optionsUnwrapped.RequestPath,
                StaticFilesPathPrefix = optionsUnwrapped.StaticFilesRequestPath
            });

            optionsUnwrapped.FileProvider = optionsUnwrapped.FileProvider ?? new BundleFileProvider(_bundleManager, httpContextAccessor);

            BundleGlobalOptions globalOptionsUnwrapped = globalOptions.Value;

            if (globalOptionsUnwrapped.EnableCacheHeader)
            {
                Action <StaticFileResponseContext> originalPrepareResponse = optionsUnwrapped.OnPrepareResponse;
                optionsUnwrapped.OnPrepareResponse = ctx =>
                {
                    Microsoft.AspNetCore.Http.Headers.ResponseHeaders headers = ctx.Context.Response.GetTypedHeaders();
                    headers.CacheControl = new CacheControlHeaderValue {
                        MaxAge = globalOptionsUnwrapped.CacheHeaderMaxAge
                    };
                    originalPrepareResponse?.Invoke(ctx);
                };
            }

            _staticFileMiddleware = new StaticFileMiddleware(next, env, options, loggerFactory);
        }