Пример #1
0
        /// <summary>
        /// The configure method called by the runtime; use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
        /// <param name="logger">The <see cref="ILogger"/>.</param>
        public void Configure(IApplicationBuilder app, ILogger <Startup> logger)
        {
            // Add exception handling to the pipeline.
            app.UseWebApiExceptionHandler(logger, _config.GetValue <bool>("BeefIncludeExceptionInInternalServerError"));

            // Add Swagger as a JSON endpoint and to serve the swagger-ui to the pipeline.
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Cdr.Banking"));

            // Add health checks page to the pipeline.
            app.UseHealthChecks("/health");

            // Add execution context set up to the pipeline.
            app.UseExecutionContext((hc, ec) =>
            {
                // TODO: This would be replaced with appropriate OAuth integration, etc... - this is purely for illustrative purposes only.
                if (!hc.Request.Headers.TryGetValue("cdr-user", out var username) || username.Count != 1)
                {
                    throw new Beef.AuthorizationException();
                }

                var bec       = (Business.ExecutionContext)ec;
                bec.Timestamp = SystemTime.Get(hc.RequestServices).UtcNow;

                switch (username[0])
                {
                case "jessica":
                    bec.Accounts.AddRange(new string[] { "12345678", "34567890", "45678901" });
                    break;

                case "jenny":
                    bec.Accounts.Add("23456789");
                    break;

                case "jason":
                    break;

                default:
                    throw new Beef.AuthorizationException();
                }

                return(Task.CompletedTask);
            });

            // Use controllers.
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
        /// <summary>
        /// Represents the default <see cref="ExecutionContext"/> update function. The <see cref="ExecutionContext.Username"/> will be set to the <see cref="System.Security.Principal.IIdentity.Name"/>
        /// from the <see cref="HttpContext"/> <see cref="HttpContext.User"/>; otherwise, <see cref="DefaultUsername"/> where <c>null</c>. The <see cref="ExecutionContext.Timestamp"/>
        /// will be set to <see cref="ISystemTime.UtcNow"/>.
        /// </summary>
        /// <param name="context">The <see cref="HttpContext"/>.</param>
        /// <param name="ec">The <see cref="ExecutionContext"/>.</param>
        public static Task DefaultExecutionContextUpdate(HttpContext context, ExecutionContext ec)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

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

            ec.Username  = context.User?.Identity?.Name ?? DefaultUsername;
            ec.Timestamp = SystemTime.Get(context.RequestServices).UtcNow;
            return(Task.CompletedTask);
        }