Exemplo n.º 1
0
        public static HttpClient GetHttpClientWithProxy(AppSettingProxy proxy)
        {
            var httpClientHandler = GetHttpClientHandlerWithProxy(proxy);
            var client            = new HttpClient(httpClientHandler, true);

            return(client);
        }
Exemplo n.º 2
0
        public static IWebProxy GetWebProxy(AppSettingProxy proxy)
        {
            IWebProxy webProxy = new WebProxy();
            bool      useProxy = proxy.Enable;

            if (useProxy)
            {
                string proxyType     = proxy.Type;
                string proxyHost     = proxy.Server;
                int    proxyPort     = proxy.Port;
                string proxyUserName = proxy.Username;
                string proxyPassword = proxy.Password;
                bool   hasAuth       = !string.IsNullOrEmpty(proxy.Username);

                if (proxyType == "http")
                {
                    webProxy = new WebProxy
                    {
                        Address               = new Uri($"http://{proxyHost}:{proxyPort}"),
                        BypassProxyOnLocal    = false,
                        UseDefaultCredentials = !hasAuth,
                    };
                    if (hasAuth)
                    {
                        webProxy.Credentials = new NetworkCredential(userName: proxyUserName, password: proxyPassword);
                    }
                }
                else if (proxyType == "socks5")
                {
                    webProxy = new HttpToSocks5Proxy(proxyHost, proxyPort);
                }
            }
            return(webProxy);
        }
Exemplo n.º 3
0
        public static HttpClientHandler GetHttpClientHandlerWithProxy(AppSettingProxy proxy)
        {
            var httpClientHandler = new HttpClientHandler()
            {
                Proxy = GetWebProxy(proxy)
            };

            return(httpClientHandler);
        }
Exemplo n.º 4
0
        public void ConfigureServices(IServiceCollection services)
        {
            var    AppSettingsSection = Configuration.GetSection("App");
            string fireBaseProjectId  = Configuration.GetValue <string>("App:FireBase:ProjectId");

            AppSettingProxy proxySettings = Configuration.GetSection("App:Proxy").Get <AppSettingProxy>();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.BackchannelHttpHandler = new HttpClientHandler
                {
                    Proxy = Helpers.Lib.HttpClientManager.GetWebProxy(proxySettings)
                };
                options.Authority = $"https://securetoken.google.com/{fireBaseProjectId}";
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer   = true,
                    ValidIssuer      = $"https://securetoken.google.com/{fireBaseProjectId}",
                    ValidateAudience = true,
                    ValidAudience    = fireBaseProjectId,
                    ValidateLifetime = true
                };
            });

            services.AddDbContext <AppDbContext>(options =>
                                                 options.UseSqlServer(Configuration.GetConnectionString("AppDb")));

            services.AddControllers();

            services.AddMediatR(typeof(Startup));
            services.AddAutoMapper(typeof(Startup));
            services.Configure <AppSettings>(AppSettingsSection);
            services.AddScoped(typeof(IPipelineBehavior <,>), typeof(TransactionBehavior <,>));
            services.AddScoped(typeof(IPipelineBehavior <,>), typeof(RequestPerformanceBehavior <,>));
            services.AddScoped(typeof(IPipelineBehavior <,>), typeof(EventLoggerBehavior <,>));

            services.AddScoped <IFireBaseTool, FireBaseTool>();

            services.AddCors(options =>
            {
                options.AddPolicy("ApiCorsPolicy",
                                  builder => builder
                                  .AllowAnyOrigin()
                                  .AllowAnyMethod()
                                  .AllowAnyHeader()
                                  //.AllowCredentials()
                                  );
            });
        }