예제 #1
0
        async Task ProcessException(HttpContext context, Exception EX)
        {
            if (ContentNegotiation.IsAcceptable(context.Request, "text/html"))
            {
                context.Response.Redirect("/Error");
                context.Response.Cookies.Append(Constants.ERROR_COOKIE_NAME, EX.Message);
                context.Response.StatusCode = 302;
            }
            else
            {
                var httpReq      = context.Request;
                var req          = $"{httpReq.Method}: {httpReq.Scheme}://{httpReq.Host}{httpReq.Path}";
                var errorMessage = EX.Message;
                if (EX.InnerException != null)
                {
                    errorMessage += ", " + EX.InnerException.Message;
                }

                // for every other case, we will return JSON errors
                var error = new ProblemDetails
                {
                    Type     = "about:blank",
                    Title    = $"error during request: {req}",
                    Status   = 500,
                    Detail   = errorMessage,
                    Instance = "{httpReq.Scheme}://{httpReq.Host}{httpReq.Path}"
                };
                var result = JsonSerializer.Serialize(error);
                context.Response.ContentType = "application/json";
                context.Response.StatusCode  = 500;
                await context.Response.WriteAsync(result);
            }
        }
예제 #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, BookmarkContext context, IOptions <JwtSettings> jwtSettings)
        {
            app.UseRouting();
            app.UseErrorHandling();
            app.UseJwtAuth();
            app.UseSwagger();
            app.UseStaticFiles();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Bookmarks API V1");
            });

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

            if (env.IsDevelopment())
            {
                context.Database.EnsureCreated();
            }

            // fallback route for html5 client routing
            // https://weblog.west-wind.com/posts/2017/Aug/07/Handling-HTML5-Client-Route-Fallbacks-in-ASPNET-Core
            app.Run(async(context) =>
            {
                // only do this for html-requesting clients
                if (ContentNegotiation.IsAcceptable(context.Request, "text/html"))
                {
                    var authenticated = context?.User?.Identity?.IsAuthenticated ?? false;
                    if (!authenticated)
                    {
                        context !.Response.Redirect(jwtSettings.Value.LoginRedirect);
                    }
                    else
                    {
                        context !.Response.ContentType = "text/html";
                        await context.Response.SendFileAsync(Path.Combine(env.WebRootPath, "ui/index.html"));
                    }
                }
            });
        }