Esempio n. 1
0
 protected override void OnStart(string[] args)
 {
     try
     {
         //#if (DEBUG)
         //     Debugger.Launch();
         //#endif
         _server = WebApp.Start <Startup>(url: _WEB_API_SERVER_ADD);
         AutoNLog.Log4Info("成功启动WebApiServer");
     }
     catch (Exception ex)
     {
         AutoNLog.Log4Exception("启动WebApiServer失败,", ex);
     }
 }
Esempio n. 2
0
 protected override void OnStop()
 {
     try
     {
         if (_server != null)
         {
             _server.Dispose();
         }
         base.OnStop();
         AutoNLog.Log4Info("成功停止WebApiServer");
     }
     catch (Exception ex)
     {
         AutoNLog.Log4Exception("停止WebApiServer失败,", ex);
     }
 }
Esempio n. 3
0
        protected virtual HttpResponseMessage RespExMsg(Exception ex)
        {
            MyLog log = new MyLog(WebApiGlobal._MyLogPath);

            log.log(ex.Message);
            AutoNLog.Log4Exception(CustomErrorMessage.发生异常.ToString(), ex);
            CustomHttpResponseMessage r = new CustomHttpResponseMessage();

            r.RespData     = "";
            r.ErrorMessage = CustomErrorMessage.操作失败.ToString();
            r.RespStatus   = CustomHttpResponseMessageStatus.Error.ToString();

            return(new HttpResponseMessage {
                Content = new StringContent(JsonConvert.SerializeObject(r, _JsonSetting), Encoding.GetEncoding("UTF-8"), "application/json")
            });
        }
Esempio n. 4
0
        //REF: http://blog.kkbruce.net/2012/05/aspnet-web-api-8-http-http-message.html
        //REF: http://bit.ly/16lpGKM
        protected override System.Threading.Tasks.Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            try
            {
                return(base.SendAsync(request, cancellationToken).ContinueWith((task) =>
                {
                    HttpResponseMessage resp = task.Result as HttpResponseMessage;
                    resp.Headers.Remove("Server");
                    resp.Headers.Remove("X-Powered-By");
                    AutoNLog.Log4Trace(request, resp);

                    return resp;
                }));
            }
            catch (Exception ex)
            {
                AutoNLog.Log4Exception(CustomErrorMessage.Trace发生异常.ToString(), ex);
                throw ex;
            }
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            try
            {
                //System.Windows.Forms.Application.Run(new Form1());
                AutoNLog.Log4Info("开始");
                using (WebApp.Start <Startup>(url: _WEB_API_SERVER_ADD))
                {
                    //HttpClient client = new HttpClient();
                    //HttpResponseMessage response = client.GetAsync("http://localhost:9527/api/Help/GetCarSize").Result;
                    //response.Headers.Remove("Server");
                    //Console.WriteLine(response);
                    //Console.WriteLine(response.Content.ReadAsStringAsync().Result);

                    AutoNLog.Log4Info("成功");
                    Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                AutoNLog.Log4Exception("WebApiSelfHost发生异常:", ex);
            }
        }
Esempio n. 6
0
        //protected virtual bool IsSysKey()
        //{
        //    try
        //    {
        //        string UserKey = this.Request.Headers.GetValues(WebApiGlobal._USERKEY).ToArray()[0];
        //        //string requestUri = this.Request.RequestUri.OriginalString;
        //        string requestUri = this.Request.Headers.GetValues(WebApiGlobal._ORI_REQUEST_URL).ToArray()[0];
        //        //string requestUri = this.Request.RequestUri.OriginalString;
        //        requestUri = System.Web.HttpUtility.UrlDecode(requestUri);
        //        DataTable dt = RAMCache.Instance.UserKeyAndSalt;
        //        DataRow[] dr = dt.Select(WebApiGlobal._USERKEY + " = '" + UserKey + "'");
        //        if (dr.Length == 1)
        //        {
        //            string keyDataBase = dr[0][WebApiGlobal._DATABASENAME].ToString();
        //            string userCode = dr[0][WebApiGlobal._USERCODE].ToString();

        //            if (keyDataBase == ChooseDataBase.System.ToString())
        //            {
        //                return true;
        //            }
        //            else
        //            {
        //                AutoNLog.Log4Warn(CustomErrorMessage.UserKey跨库使用.ToString() + ",UserCode:" + userCode + ",RequestUri:" + requestUri);
        //                return false;
        //            }
        //        }
        //        else
        //        {
        //            AutoNLog.Log4Warn(CustomErrorMessage.UserKey无效.ToString() + ",UserKey:" + UserKey + ",RequestUri:" + requestUri);
        //            return false;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        AutoNLog.Log4Exception(CustomErrorMessage.发生异常.ToString(), ex);
        //        return false;
        //    }
        //}
        //protected virtual bool VerifyUserKey(Params4ApiCRUD P)
        //{
        //    string UserKey = this.Request.Headers.GetValues(WebApiGlobal._USERKEY).ToArray()[0];
        //    //string SHA512UserKey = Encryption.Instance.StringToSHA512Hash(UserKey);
        //    DataTable dt = RAMCache.Instance.UserKeyAndSalt;
        //    DataRow[] dr = dt.Select(WebApiGlobal._USERKEY + " = '" + UserKey + "'");
        //    string chooseDataBase = DataBaseConnFactory.Instance.GetUserKeyAndDataBase(P.chooseDataBase);
        //    string keyDataBase = dr[0][WebApiGlobal._DATABASENAME].ToString();
        //    string userCode = dr[0][WebApiGlobal._USERCODE].ToString();
        //    if (chooseDataBase == keyDataBase)
        //    {
        //        P.UserCode = userCode;
        //        return true;
        //    }
        //    else
        //    {
        //        string ClientTS = this.Request.Headers.GetValues(WebApiGlobal._TIMESPAN).ToArray()[0];

        //        //string requestUri = this.Request.RequestUri.AbsoluteUri;
        //        string requestUri = this.Request.RequestUri.OriginalString;
        //        requestUri = System.Web.HttpUtility.UrlDecode(requestUri);

        //        string str4Log = "ChooseDataBase:" + chooseDataBase + ",UserCode:" + userCode + ",ClientTS:" + ClientTS + ",requestUri:" + requestUri;

        //        AutoNLog.Log4Warn(CustomErrorMessage.UserKey跨库使用.ToString() + str4Log);
        //        return false;
        //    }
        //}

        protected virtual bool VerifyUserKey(Params4ApiCRUD P, decimal timespan = 600)//默认允许时差十分钟
        {
            try
            {
                bool   iscorrect = false;
                string ClientTS  = this.Request.Headers.GetValues(WebApiGlobal._TIMESPAN).ToArray()[0];
                string ServerTS  = CommonMethod.UTCTS;
                //string requestUri = this.Request.RequestUri.AbsoluteUri;
                string requestUri = this.Request.Headers.GetValues(WebApiGlobal._ORI_REQUEST_URL).ToArray()[0];
                //string requestUri = this.Request.RequestUri.OriginalString;
                requestUri = System.Web.HttpUtility.UrlDecode(requestUri);
                string UserKey = this.Request.Headers.GetValues(WebApiGlobal._USERKEY).ToArray()[0];

                string chooseDataBase = P.chooseDataBase.ToString();

                DataTable dt         = RAMCache.Instance.UserKeyAndSalt;
                DataRow[] dr         = dt.Select(WebApiGlobal._USERKEY + " = '" + UserKey + "'");
                string    str4ErrLog = "";
                if (dr.Length == 1)
                {
                    string UserCode    = dr[0][WebApiGlobal._USERCODE].ToString();
                    string UserSalt    = dr[0][WebApiGlobal._USERSALT].ToString();
                    string OriKey      = dr[0][WebApiGlobal._DECODE_USERKEY].ToString();
                    string KeyDataBase = dr[0][WebApiGlobal._DATABASENAME].ToString();
                    str4ErrLog = "UserCode:" + UserCode + ",ClientTS:" + ClientTS + ",requestUri:" + requestUri;

                    decimal tsc  = ClientTS.ToDecimalEx(0);
                    decimal tss  = ServerTS.ToDecimalEx(0);
                    decimal diff = tss - tsc;
                    if (diff > timespan || diff < -5)
                    {
                        AutoNLog.Log4Warn(CustomErrorMessage.TimeSpan错误.ToString() + str4ErrLog);
                        return(iscorrect);
                    }

                    if (chooseDataBase != KeyDataBase)
                    {
                        AutoNLog.Log4Warn(CustomErrorMessage.UserKey跨库使用.ToString() + "ChooseDataBase:" + chooseDataBase + str4ErrLog);
                        return(iscorrect);
                    }

                    string ClientSHA256Sign = this.Request.Headers.GetValues(WebApiGlobal._SHA256).ToArray()[0];
                    string ServerSHA256Sign = CommonMethod.StringToSHA256Hash(OriKey + requestUri + ClientTS + UserSalt);
                    if (ClientSHA256Sign != ServerSHA256Sign)
                    {
                        AutoNLog.Log4Warn(CustomErrorMessage.Hash校验错误.ToString() + str4ErrLog + ",ClientSHA256Sign:" + ClientSHA256Sign + ",ServerSHA256Sign:" + ServerSHA256Sign);
                        return(iscorrect);
                    }
                    P.UserCode = UserCode;
                    iscorrect  = true;
                    return(iscorrect);
                }
                else
                {
                    AutoNLog.Log4Warn(CustomErrorMessage.UserKey无效.ToString() + UserKey);
                    return(iscorrect);
                }
            }
            catch (Exception ex)
            {
                AutoNLog.Log4Exception(CustomErrorMessage.Hash校验异常.ToString(), ex);
                return(false);
            }
        }