// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env , ILoggerFactory loggerFactory) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // 按照规则名称应用跨域规则,必须在 UseRouting 前应用 app.UseCors("global_cors"); app.UseStaticFiles(); app.UseRouting(); // 管道中应用认证中间件 app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); // 设置 MyHttpContext var accessor = app.ApplicationServices.GetRequiredService <IHttpContextAccessor>(); MyHttpContext.Configure(accessor); // 设置日志处理,需要安装 Microsoft.Extensions.Logging.Log4Net.AspNetCore 包 loggerFactory.AddLog4Net(); }
public void GetRequestIdShouldReturnNewGuidIfHeaderNotAGuid() { var subject = new MyHttpContext("notaguid"); var result = subject.GetRequestId(); Assert.AreNotEqual(Guid.Empty, result); }
public void TryGetRequestIdShouldReturnFalseIfHeaderNotAGuid() { var subject = new MyHttpContext("notaguid"); Guid resultGuid; var result = subject.TryGetRequestId(out resultGuid); Assert.IsFalse(result); }
public void GetRequestIdShouldReturnTheGuidIfSet() { var expectedGuid = new Guid("88887777-6666-5555-4444-333322221111"); var subject = new MyHttpContext(expectedGuid.ToString("N")); var result = subject.GetRequestId(); Assert.AreEqual(expectedGuid, result); }
public void TryGetRequestIdShouldReturnFalseIfHeaderNotAGuid() { var subject = new MyHttpContext("notaguid"); Guid resultGuid; var result = subject.TryGetRequestId(out resultGuid); Assert.IsFalse(result); }
public void TryGetRequestIdShouldReturnFalseIfNoHeader() { var subject = new MyHttpContext(null); Guid resultGuid; var result = subject.TryGetRequestId(out resultGuid); Assert.IsFalse(result); }
public void GetRequestIdShouldReturnTheGuidIfSet() { var expectedGuid = new Guid("88887777-6666-5555-4444-333322221111"); var subject = new MyHttpContext(expectedGuid.ToString("N")); var result = subject.GetRequestId(); Assert.AreEqual(expectedGuid, result); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); MyIoc.SetServiceCollection(services); MyHttpContext.SetServiceCollection(services); services.RegisterAssemblyInterfaces("ToolGood.SqlOnline.Application", null, LifeStyle.PerLifetimeScope); services.AddAntiforgery(options => { options.HeaderName = "__RequestVerificationToken"; options.FormFieldName = "__RequestVerificationToken"; }); services.AddResponseCompression(options => { options.Providers.Add <BrotliCompressionProvider>(); options.Providers.Add <GzipCompressionProvider>(); options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "image/svg+xml", "application/x-font-truetype" }); }); services.AddDistributedMemoryCache(); services.AddSession(options => { options.Cookie.Name = "sid"; options.IdleTimeout = TimeSpan.FromHours(3); options.IOTimeout = TimeSpan.FromSeconds(1); options.Cookie.IsEssential = true; options.Cookie.HttpOnly = true; options.Cookie.Path = "/"; //options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.None; //猎豹浏览器 ajax 请求无效 //options.Cookie.SecurePolicy = Microsoft.AspNetCore.Http.CookieSecurePolicy.SameAsRequest; }); services.Configure <CookiePolicyOptions>(options => { options.CheckConsentNeeded = context => false; //options.MinimumSameSitePolicy = Microsoft.AspNetCore.Http.SameSiteMode.None; //猎豹浏览器 ajax 请求无效 //options.Secure = Microsoft.AspNetCore.Http.CookieSecurePolicy.SameAsRequest; }); services.AddHttpContextAccessor(); services.AddHttpClient(); services.AddControllersWithViews(options => { options.Filters.Add <HttpGlobalExceptionFilter>(); }).AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) .AddDataAnnotationsLocalization() .AddNewtonsoftJson(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; options.SerializerSettings.TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple; options.SerializerSettings.Converters.Add(new JsonCustomDoubleConvert()); // json序列化时, 防止double,末尾出现小数点浮动, options.SerializerSettings.Converters.Add(new JsonCustomDoubleNullConvert()); // json序列化时, 防止double,末尾出现小数点浮动, }); services.AddRazorPages(); services.AddControllers(); services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All)); }
public void TryGetRequestIdShouldReturnTheGuidIfSet() { var expectedGuid = new Guid("88887777-6666-5555-4444-333322221111"); var subject = new MyHttpContext(expectedGuid.ToString("N")); Guid actualGuid; var result = subject.TryGetRequestId(out actualGuid); Assert.IsTrue(result); Assert.AreEqual(expectedGuid, actualGuid); }
public void ProcessRequest(MyHttpContext context) { string str = $"<!DOCTYPE html><html lang=en> <head><meta charset=UTF-8>" + $"<meta name=viewport content=\"width = device - width, initial - scale = 1.0\">" + $"<title>Hello World Aspx from C#</title></head> " + $"<body> <h1>Hello world! C# Aspx</h1>" + $"<h2>ReqTime: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}</2> " + $"</body> </html>"; context.Response.StateCode = "200"; context.Response.StateDes = "OK"; context.Response.ContentType = "text/html"; context.Response.Body = Encoding.UTF8.GetBytes(str); }
public void ProcessRequest(MyHttpContext context) { string str = $"<!DOCTYPE html><html lang=en> <head><meta charset=UTF-8>" + $"<meta name=viewport content=\"width = device - width, initial - scale = 1.0\">" + $"<title> C# webapi</title></head> " + $"<body> <h1>Weocome to C# webapi page!</h1>" + $"<h2>ReqTime: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}</h2> " + $"<h3>CurrentDomain:{AppDomain.CurrentDomain.ToString()}</h3> " + $"</body> </html>"; context.Response.StateCode = "200"; context.Response.StateDes = "OK"; context.Response.ContentType = "text/html"; context.Response.Body = Encoding.UTF8.GetBytes(str); }
public static IFunctionHttpContextAccessor EstablishHttpContextAccessor( ExecutionContext context, HttpRequestMessage reqMessage, HttpRequest req ) { var factory = GetFactory(context); var httpAccessor = factory.ServiceProvider.GetService(typeof(IFunctionHttpContextAccessor)) as IFunctionHttpContextAccessor; var response = new MyHttpResponse(req); var httpContext = new MyHttpContext(factory.ServiceProvider, req, response); httpContext.SetIdentityServerBasePath("/api/authority"); httpAccessor.HttpContext = httpContext; httpAccessor.HttpRequestMessage = reqMessage; httpAccessor.HttpResponseMessage = reqMessage.CreateResponse(); return(httpAccessor); }
public static IApplicationBuilder UseCustomHttpContextAccessor(this IApplicationBuilder app) { MyHttpContext.Configure(app.ApplicationServices.GetRequiredService <IHttpContextAccessor>()); return(app); }
public void TryGetRequestIdShouldReturnTheGuidIfSet() { var expectedGuid = new Guid("88887777-6666-5555-4444-333322221111"); var subject = new MyHttpContext(expectedGuid.ToString("N")); Guid actualGuid; var result = subject.TryGetRequestId(out actualGuid); Assert.IsTrue(result); Assert.AreEqual(expectedGuid, actualGuid); }
public void TryGetRequestIdShouldReturnFalseIfNoHeader() { var subject = new MyHttpContext(null); Guid resultGuid; var result = subject.TryGetRequestId(out resultGuid); Assert.IsFalse(result); }
public void GetRequestIdShouldReturnNewGuidIfNoHeader() { var subject = new MyHttpContext(null); var result = subject.GetRequestId(); Assert.AreNotEqual(Guid.Empty, result); }