Exemplo n.º 1
0
        private AppInfoDTO TODTO(AppInfo appInfo)
        {
            AppInfoDTO dto = new AppInfoDTO();

            dto.AppSecret = appInfo.AppSecret;
            dto.Name      = appInfo.Name;
            dto.IsEnabled = appInfo.IsEnabled;
            return(dto);
        }
        private AppInfoDTO TODTO(AppInfoEntity entity)
        {
            AppInfoDTO dto = new AppInfoDTO();

            dto.Id        = entity.Id;
            dto.AppKey    = entity.AppKey;
            dto.AppSecret = entity.AppSecret;
            return(dto);
        }
        AppInfoDTO ToDTO(T_AppInfo appInfo)
        {
            if (appInfo == null)
            {
                return(null);
            }
            AppInfoDTO dto = new AppInfoDTO();

            dto.AppKey    = appInfo.AppKey;
            dto.AppSecret = appInfo.AppSecret;
            dto.Id        = appInfo.Id;
            dto.Name      = appInfo.Name;
            dto.IsEnabled = appInfo.IsEnabled;
            return(dto);
        }
Exemplo n.º 4
0
        public async Task <AppInfoDTO> GetByAppKeyAsync(string appKey)
        {
            using (MyDbContext ctx = new MyDbContext())
            {
                var appInfo = await ctx.Appinfos.FirstOrDefaultAsync(e => e.AppKey == appKey);

                if (appInfo == null)
                {
                    return(null);
                }
                else
                {
                    AppInfoDTO dto = new AppInfoDTO();
                    dto.AppKey    = appInfo.AppKey;
                    dto.AppSecret = appInfo.AppSecret;
                    dto.Id        = appInfo.Id;
                    dto.Name      = appInfo.Name;
                    dto.IsEnabled = appInfo.IsEnabled;
                    return(dto);
                }
            }
        }
        /// <summary>
        /// 获取AppInfo信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private async Task <AppInfoDTO> GetDTO(string key)
        {
            AppInfoDTO dto = new AppInfoDTO();

            using (HttpClient client = new HttpClient())
            {
                var res = await client.GetAsync($"http://localhost:8000/api/AppInfo/Get?appKey={key}");

                if (res.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    return(null);
                }
                var json = await res.Content.ReadAsStringAsync();

                JObject obj = JsonConvert.DeserializeObject <JObject>(json);
                if (obj["data"] == null)
                {
                    return(null);
                }
                dto = JsonConvert.DeserializeObject <AppInfoDTO>(obj["data"].ToString());
                return(dto);
            }
        }
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            ApiAuthorAttribute[] attris = (ApiAuthorAttribute[])((ControllerActionDescriptor)context.ActionDescriptor).MethodInfo.GetCustomAttributes(typeof(ApiAuthorAttribute), false);
            if (attris.Length > 0)
            {
                return;
            }


            //从报文头中获取Appkey和sign
            StringValues appkeys;
            StringValues signs;

            if (!context.HttpContext.Request.Headers.TryGetValue("appKey", out appkeys))
            {
                context.Result = new JsonResult(new APIResult <int> {
                    ErrorMsg = "appkey不存在"
                })
                {
                    StatusCode = 400
                };
                return;
            }
            if (!context.HttpContext.Request.Headers.TryGetValue("sign", out signs))
            {
                context.Result = new JsonResult(new APIResult <int> {
                    ErrorMsg = "sign不存在"
                })
                {
                    StatusCode = 400
                };
                return;
            }
            string sign = signs.First();
            string key  = appkeys.First();

            //获取AppInfo
            //var task = GetDTO(key);
            //task.Wait();
            AppInfoDTO dto = new AppInfoDTO {
                AppKey = "*****@*****.**", AppSecret = "0cc8e711-81e3-43ba-aeff-8fab8212efd2"
            };

            if (dto == null)
            {
                context.Result = new JsonResult(new APIResult <int> {
                    ErrorMsg = "appkey错误"
                })
                {
                    StatusCode = 401
                };
                return;
            }
            if (key != dto.AppKey)
            {
                context.Result = new JsonResult(new APIResult <int> {
                    ErrorMsg = "appkey错误"
                })
                {
                    StatusCode = 401
                };
                return;
            }
            //计算Sign
            string thSign = CalceSign(context, dto.AppSecret);

            if (!sign.Equals(thSign, StringComparison.InvariantCultureIgnoreCase))
            {
                context.Result = new JsonResult(new APIResult <int> {
                    ErrorMsg = "sign错误"
                })
                {
                    StatusCode = 401
                };
                return;
            }
        }