示例#1
0
        public string Encode(JwtToken token)
        {
            // 基于(提供给客户端的)基础信息编码
            string header  = EncodingExt.ToBase64Url(SerializeExt.ToJson(token.Header));
            string payload = EncodingExt.ToBase64Url(SerializeExt.ToJson(token.Payload));

            // 生成签名
            string signature = EncodingExt.ToBase64Url(ComputeHash(header, payload, token.Salt));

            return($"{header}.{payload}.{signature}");
        }
示例#2
0
        /// <summary>
        /// 处理请求
        /// </summary>
        private Response Handle(string serviceName, string methodName, string httpType)
        {
            // 缓存请求头
            _httpHeaders = new Dictionary <string, string>
            {
                { "Authorization", Request.GetHeader("Authorization") ?? "" }
            };

#if DEBUG
            _log.Debug(Request.Url);
#endif

            Type interfaceType;
            var  service = Ioc.ResolveAppService(serviceName, out interfaceType);
            if (service == null)
            {
                return("Invalid Service");
            }

            var methodInfo = interfaceType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public);

            try
            {
                // 校验
                CheckRequest(methodInfo, httpType);

                // 若校验通过,则更新Token,防止重复请求
                UpdateToken();

                var res = InvokeMethod(methodInfo, service);

#if DEBUG
                _log.Debug(">>" + SerializeExt.ToJson(res, false));
#endif

                return(Response.AsJson(ApiResponse.SuccessResponse(res)));
            }
            catch (BusinessException ex)
            {
                _log.Info(ex, "业务异常");
                return(Response.AsJson(ApiResponse.FailResponse(ex.Message)));
            }
            catch (AuthException ex)
            {
                _log.Warning(ex, "身份验证异常");
                return(Response.AsJson(ApiResponse.FailResponse(ex.Message), HttpStatusCode.Unauthorized));
            }
            catch (Exception ex)
            {
                _log.Error(ex, "请求发生异常");
                return(Response.AsJson(ApiResponse.FailResponse("系统异常"), HttpStatusCode.InternalServerError));
            }
        }
示例#3
0
        public JwtToken Decode(string tokenStr)
        {
            string[] parts = tokenStr.Split(".");

            var header  = SerializeExt.JsonTo <JwtHeader>(EncodingExt.FromBase64Url(parts[0], null));
            var payload = SerializeExt.JsonTo <JwtPayload>(EncodingExt.FromBase64Url(parts[1], null));

            return(new JwtToken
            {
                Header = header,
                Payload = payload
            });
        }
示例#4
0
        /// <summary>
        /// 保存填写的数据库信息
        /// </summary>
        public void SaveConfig()
        {
            RefreshHistoryDic();

            // 新增当前信息
            _dicHistory.SafeAdd($"{DB.Server}@{DB.Database}", (DataBase)DB);

            // 序列化保存
            List <DataBase> dbList = _dicHistory.Select(e => e.Value).ToList();

            LogExt.Exec(() =>
            {
                SerializeExt.ToXml(dbList, ConfigFilePath);
            });
        }
示例#5
0
        private void RefreshHistoryDic()
        {
            // 反序列化
            var dbList = LogExt.Exec
                         (
                () => SerializeExt.XmlTo <List <DataBase> >(GetConfigXml().InnerXml),
                () => new List <DataBase>()
                         );

            // 绑定
            _dicHistory.Clear();
            if (dbList.Count > 0)
            {
                dbList.OrderByDescending(db => db.UpdateTime)
                .Take(10)
                .ForEach(db => _dicHistory.Add($"{db.Server}@{db.Database}", db));
            }
        }
示例#6
0
        public static object GetValue(this Request requset, string name, Type type)
        {
            if (type != typeof(string) && type.IsClass)
            {
                return(requset.GetObject(name, type));
            }

            string value = requset.Form[name];

            if (value == null)
            {
                value = requset.Query[name];
            }
            if (value == null)
            {
                throw new Exception("不能获取名称为" + name + "的值");
            }

            if (type == typeof(string) ||
                type == typeof(char) ||
                type == typeof(bool) ||
                type == typeof(byte) ||
                type == typeof(short) ||
                type == typeof(int) ||
                type == typeof(uint) ||
                type == typeof(long) ||
                type == typeof(ulong) ||
                type == typeof(float) ||
                type == typeof(double) ||
                type == typeof(decimal) ||
                type == typeof(Guid) ||
                type == typeof(DateTime))
            {
                return(TypeExt.ConvertType(type, value));
            }

            return(SerializeExt.JsonTo(value, type));
        }
示例#7
0
 public virtual void SaveConfig(string path)
 {
     SerializeExt.XmlSerialize(SysList, path);
 }
示例#8
0
 public virtual void LoadConfig(string path)
 {
     SysList = SerializeExt.XmlDeserialize <Config>(path, true);
 }