Пример #1
0
        public Object Get([FromHeader] string Authorization)
        {
            //CountryDatabase.connect();
            //return Ok(CountryDatabase.getAllCountryCode());


            string response = OAuth2Server.ValidateAccessToken(Authorization);

            if (response.Equals("Valid"))
            {
                CountryDatabase.connect();
                return(Ok(CountryDatabase.getAllCountryCode()));//new string[] { "value1", "value2" };}
            }
            else if (response.Equals("Missing access_token"))
            {
                errorModel err = new errorModel();
                err.error         = new error();
                err.error.status  = 401;
                err.error.message = "Missing access_token";
                return(Ok(err));
            }
            else if (response.Equals("Expired access_token"))
            {
                HttpContext.Response.StatusCode = 401;
                errorModel err = new errorModel();
                err.error         = new error();
                err.error.message = "Expired access_token";
                err.error.status  = 401;
                return(err);
            }
            else
            {
                return(Unauthorized());
            }
        }
Пример #2
0
    /// <summary>
    /// 异常信息写入日志文件
    /// </summary>
    /// <param name="ex">异常信息</param>
    /// <param name="jo">相关内容</param>
    /// <param name="ctx">请求上下文</param>
    public static void RecordErrorToFile(Exception ex, string info, HttpContext ctx)
    {
        try
        {
            errorModel objError = new errorModel();
            objError.apiName = ctx.Request.Url.AbsoluteUri;

            objError.ex   = ex;
            objError.para = info;

            JObject joError = JObject.Parse(JsonConvert.SerializeObject(objError));

            //取得当前需要写入的日志文件名称及路径
            string strFullPath = "";
            string logPath     = "";


            logPath     = HttpContext.Current.Server.MapPath(AppConfig.strLogPath);
            strFullPath = HttpContext.Current.Server.MapPath(AppConfig.strLogPath + @"\" + AppConfig.strErrorFileName);


            //取得异常信息的内容
            string strTime      = "\r\n------BEGIN----------------------------" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "------------------------------\r\n";
            string logErrorInfo = strTime + joError.ToString();
            logErrorInfo += ("\r\n------END-----------------------------------------------------------------------------\r\n");

            //执行写入
            //检查 Log 文件所存放的目录是否存在,如果不存在,建立该文件夹
            if (!Directory.Exists(logPath))
            {
                Directory.CreateDirectory(logPath);
            }

            //判断当前的日志文件是否创建,如果未创建,执行创建并加入异常内容;
            //如果已经创建则直接追加填写
            if (!File.Exists(strFullPath))
            {
                using (StreamWriter sw = File.CreateText(strFullPath))
                {
                    sw.Write(logErrorInfo);
                    sw.Flush();
                }
            }
            else
            {
                using (StreamWriter sw = File.AppendText(strFullPath))
                {
                    sw.Write(logErrorInfo);
                    sw.Flush();
                }
            }
        }
        catch
        {
            return;
        }
    }