// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { var handler = new GatewayHttpClientHandler(new Uri("fabric:/Hosting/CounterService"), message => message.GetHashCode()); var proxyOptions = new ProxyOptions() { Host = "unused", BackChannelMessageHandler = handler }; app.RunProxy(proxyOptions); }
/// <summary> /// Sends request to remote server as specified in options /// </summary> /// <param name="app"></param> /// <param name="options">Options for setting port, host, and scheme</param> /// <returns></returns> public static IApplicationBuilder RunProxy(this IApplicationBuilder app, ProxyOptions options) { if (app == null) { throw new ArgumentNullException(nameof(app)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } return app.UseMiddleware<ProxyMiddleware>(options); }
public void Configure(IApplicationBuilder app) { const string scheme = "https"; const string host = "example.com"; const string port = "443"; var options = new ProxyOptions() { Scheme = scheme, Host = host, Port = port }; app.RunProxy(options); }
/// <summary> /// Sends request to remote server as specified in options /// </summary> /// <param name="app"></param> /// <param name="configureOptions">Configure options for setting port, host, and scheme</param> /// <returns></returns> public static IApplicationBuilder RunProxy(this IApplicationBuilder app, Action<ProxyOptions> configureOptions) { if (app == null) { throw new ArgumentNullException(nameof(app)); } if (configureOptions == null) { throw new ArgumentNullException(nameof(configureOptions)); } var options = new ProxyOptions(); configureOptions(options); return app.UseMiddleware<ProxyMiddleware>(options); }
public ProxyMiddleware(RequestDelegate next, ProxyOptions options) { if (next == null) { throw new ArgumentNullException(nameof(next)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } _next = next; if (string.IsNullOrEmpty(options.Host)) { throw new ArgumentException("Options parameter must specify host.", "options"); } // Setting default Port and Scheme if not specified if (string.IsNullOrEmpty(options.Port)) { if (string.Equals(options.Scheme, "https", StringComparison.OrdinalIgnoreCase)) { options.Port = "443"; } else { options.Port = "80"; } } if (string.IsNullOrEmpty(options.Scheme)) { options.Scheme = "http"; } _options = options; _httpClient = new HttpClient(_options.BackChannelMessageHandler ?? new HttpClientHandler()); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.MinimumLevel = LogLevel.Information; loggerFactory.AddConsole(); // Configure the HTTP request pipeline. // Add the following to the request pipeline only in development environment. if (env.IsDevelopment()) { app.UseErrorPage(); app.UseDatabaseErrorPage(DatabaseErrorPageOptions.ShowAll); } else { // Add Error handling middleware which catches all application specific errors and // sends the request to the following path or controller action. app.UseErrorHandler("/Home/Error"); } // Add static files to the request pipeline. app.UseStaticFiles(); // Add cookie-based authentication to the request pipeline. app.UseIdentity(); // Add authentication middleware to the request pipeline. You can configure options such as Id and Secret in the ConfigureServices method. // For more information see http://go.microsoft.com/fwlink/?LinkID=532715 // app.UseFacebookAuthentication(); // app.UseGoogleAuthentication(); // app.UseMicrosoftAccountAuthentication(); // app.UseTwitterAuthentication(); app.Use(next => { return async context => { var DbContext = (ApplicationDbContext)context.RequestServices.GetService(typeof(ApplicationDbContext)); var request = new Request(); if (!context.Request.Path.ToString().Contains("secret-route")) { var url = $"{context.Request.Scheme}://{host}:{ context.Connection.RemotePort} { context.Request.PathBase}{ context.Request.Path}{ context.Request.QueryString}"; request.MethodType = context.Request.Method.ToString(); request.Url = url; request.TimeStamp = DateTime.Now; DbContext.Add(request); DbContext.SaveChanges(); } await next(context); }; }); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "secret-route/{controller=Home}/{action=Index}/{id?}"); }); var options = new ProxyOptions() { Scheme = "https", Host = host, Port = "443" }; app.RunProxy(options); }