コード例 #1
0
        public async Task <IActionResult> Login([FromBody] UserForLoginDTO userForLoginDTO)
        {
            var userFromRepo = await _repo
                               .Login(userForLoginDTO.Username.ToLower(), userForLoginDTO.Password);

            if (userFromRepo == null)
            {
                return(Unauthorized());
            }

            var tokenGenerator = new JWTGenerator(_config);

            var user = _mapper.Map <UserForListDto>(userFromRepo);

            // O token gerado é enviado para o cliente.
            return(Ok(new {
                token = tokenGenerator.GenerateToken(userFromRepo.Username, userFromRepo.Id),
                user
            }));
        }
コード例 #2
0
ファイル: WebHooksProcess.cs プロジェクト: RcBuilder/Scripts
        private void HandleBrokerHook(BrokerData brokerData, WebHookData hookData)
        {
            var step = 0;

            try
            {
                step = 1;

                var httpService = new HttpServiceHelper();
                var config      = new DataUpdatesConfig {
                    FromTime = hookData.LastExecutionTime
                };

                var serviceData = this.BLL.GetServiceData(hookData.ServiceName, brokerData);
                if (serviceData == null)
                {
                    throw new Exception($"No Service Data: {hookData.ServiceName}");
                }

                step = 2;

                var proxyURL  = this.Config.ProxyServerURI; // "http://proxy.crtv.co.il";
                var proxyPort = serviceData.Port;

                var generator = new JWTGenerator(this.Config.JWTSecretKey);
                var token     = generator.GenerateToken(new {
                    brokerName = brokerData.Name,
                    role       = "Broker"
                });

                step = 3;

                var updatesURI    = $"{proxyURL}:{proxyPort}/{hookData.ServiceName}/updates";
                var serviceResult = httpService.POST(updatesURI, config, headers: new Dictionary <string, string>
                {
                    ["Content-Type"]  = "application/json",
                    ["Authorization"] = $"Bearer {token}"
                });

                step = 4;

                // TODO ->> StatusCode
                if (!serviceResult.Success)
                {
                    // remove html content
                    if (serviceResult.Content.Contains("<!DOCTYPE"))
                    {
                        serviceResult.Content = serviceResult.Content.Split('<').FirstOrDefault();
                    }

                    throw new Exception($"Get Updates from {updatesURI} -> {serviceResult.Content}");
                }

                var updates    = serviceResult.Content;
                var hasUpdates = !string.IsNullOrEmpty(updates) && updates != "[]";

                step = 5;

                // push updates
                if (hasUpdates)
                {
                    this.Logger.Info("WebHooksProcess", $"#{hookData.Id}, POST {hookData.ServiceName} Updates to {hookData.HookURL}");

                    var pushResult = httpService.POST($"{hookData.HookURL}", updates, headers: new Dictionary <string, string>
                    {
                        ["Content-Type"] = "application/json"
                    });

                    step = 6;

                    if (!pushResult.Success)
                    {
                        throw new Exception($"Push Updates to {hookData.HookURL} -> {serviceResult.Content}");
                    }
                }

                step = 7;

                hookData.LastExecutionTime = DateTime.Now; // update the last execution time
                this.BLL.UpdateWebHookLastExecutionTime(hookData);
            }
            catch (Exception ex)
            {
                ex.Data.Add("Method", "WebHooksProcess.HandleBrokerHook");
                ex.Data.Add("HookId", hookData.Id);
                ex.Data.Add("BrokerName", hookData.BrokerName);
                ex.Data.Add("ServiceName", hookData.ServiceName);
                ex.Data.Add("Step", step);
                this.Logger.Error("WebHooksProcess", ex);
            }
        }