/// <summary> /// 获取实体信息 /// </summary> /// <returns></returns> public static EntityInfo GetEntityInfo <T>() where T : BaseEntity { var t = typeof(T); if (!MemoryCacheHelper.Exists(t.FullName)) { var entityInfo = new EntityInfo(); var tableName = t.Name; var obs = t.GetTypeInfo().GetCustomAttribute <TableAttribute>(); if (obs != null) { tableName = obs.Name; } var fields = new List <string>(); var properties = t.GetProperties(BindingFlags.Public | BindingFlags.Instance); var dbProps = new List <PropertyInfo>(); foreach (var p in properties) { if (p.CanWrite && p.CanRead && p.GetCustomAttribute <NotMappedAttribute>() == null) { fields.Add(p.Name); dbProps.Add(p); } } entityInfo.TableName = tableName; entityInfo.Properties = dbProps.ToArray(); entityInfo.Columns = fields; MemoryCacheHelper.SetCache(t.FullName, entityInfo, TimeSpan.FromDays(1)); } return(MemoryCacheHelper.GetCache <EntityInfo>(t.FullName)); }
public string AddUser(UserInputDto userInputDto) { userInputDto.RegDate = DateTime.Now; userInputDto.CreateTime = DateTime.Now; userInputDto.LoginNum = 0; string ip = HttpContext.Connection.RemoteIpAddress.ToString(); string validateCode = MemoryCacheHelper.GetCache(ip).ToString(); //if (!string.IsNullOrEmpty(validateCode) && validateCode.ToLower() == userInputDto.validateCode) //{ Users user; user = _accountService.GetUserByQQ(userInputDto.QQ); if (user != null) { HttpContext.Response.StatusCode = 214; return("hasQQ"); } int row = _accountService.CreateAndUpdateUsers(userInputDto); if (row >= 1) { return("success"); } else { HttpContext.Response.StatusCode = 214; return("UnknowErr"); } //} //else //{ // HttpContext.Response.StatusCode = 214; // return "ValidateErr"; //} }
/// <summary> /// Action执行中触发委托 /// </summary> /// <param name="filterContext"></param> public override void OnActionExecuting(ActionExecutingContext filterContext) { string code = filterContext.HttpContext.Request.Query["code"].ToString(); var wxPubInfo = MemoryCacheHelper.GetCache <Wx_PublicInfo>("WxPubInfo"); if (null != code && code.Length > 0) { ModelWxUserInfo mWxUserInfo = WXOAuthApiHelper.GetUserInfo(wxPubInfo.AppId, wxPubInfo.AppSecret, code); if (mWxUserInfo != null) { filterContext.HttpContext.Response.Cookies.Append(ComConst.Wx_ModelWxUserInfo, JsonHelper.ToJson(mWxUserInfo), ComHelper.GetCookieOpetion()); filterContext.HttpContext.Session.SetString(ComConst.Wx_ModelWxUserInfo, JsonHelper.ToJson(mWxUserInfo)); } } else { if (filterContext.HttpContext.Request.Cookies.TryGetValue(ComConst.Wx_ModelWxUserInfo, out string value)) { filterContext.HttpContext.Response.Cookies.Append(ComConst.Wx_ModelWxUserInfo, value, ComHelper.GetCookieOpetion()); filterContext.HttpContext.Session.SetString(ComConst.Wx_ModelWxUserInfo, value); } else { var rst = new ContentResult(); rst.Content = "登录过期,请退出重新进入"; filterContext.Result = rst; } } }
public void OnActionExecuting(ActionExecutingContext context) { //get cookie var token = _cookie.GetCookie(ClaimTypes.Sid)?.ToString(); if (token == null) { context.Result = new ContentResult() { Content = "Unorthorized", }; return; } //get cache var user = _memoryCache.GetCache(token); if (user == null) { var _adminToken = new adminSystemContext().AdminTokens; var _adminUser = new adminSystemContext().AdminUsers; var id = _adminToken.Where(o => o.Token == token).FirstOrDefault().UserId; var sysUser = _adminUser.Where(o => o.Id == id); _memoryCache.SetCache(token, sysUser); if (sysUser == null) { context.Result = new ContentResult() { Content = "Unorthorized", }; return; } } }
/// <summary> /// 验证授权 /// </summary> /// <param name="httpContext"></param> /// <returns></returns> public Task Invoke(HttpContext httpContext) { var headers = httpContext.Request.Headers; //检测是否包含'Authorization'请求头,如果不包含返回context进行下一个中间件,用于访问不需要认证的API if (!headers.ContainsKey("Authorization")) { return(_next(httpContext)); } var tokenStr = headers["Authorization"]; try { string jwtStr = tokenStr.ToString().Substring("Bearer ".Length).Trim(); //验证缓存中是否存在该jwt字符串 if (!MemoryCacheHelper.Exists(jwtStr)) { return(httpContext.Response.WriteAsync("非法请求")); } JwtToken tm = ((JwtToken)MemoryCacheHelper.GetCache(jwtStr)); //提取JwtToken中的Sub属性进行authorize认证 List <Claim> lc = new List <Claim>(); Claim c = new Claim(tm.Sub + "Type", tm.Sub); lc.Add(c); ClaimsIdentity identity = new ClaimsIdentity(lc); ClaimsPrincipal principal = new ClaimsPrincipal(identity); httpContext.User = principal; return(_next(httpContext)); } catch (Exception) { return(httpContext.Response.WriteAsync("token验证异常")); } }
public string CheckLogin(string qq, string pwd, string validateString) { string ip = HttpContext.Connection.RemoteIpAddress.ToString(); //MemoryCacheHelper.SetCache(ip, "test"); string validate = MemoryCacheHelper.GetCache(ip).ToString(); if (validate != null && validateString.ToLower() == validate.ToLower()) { return(CheckStatus(qq, pwd)); } else { HttpContext.Response.StatusCode = 214; return("验证码错误"); } }
/// <summary> /// 获取模型属性列表 /// </summary> /// <param name="t"></param> /// <returns></returns> public static PropertyInfo[] GetModelProperties(Type t) { if (!MemoryCacheHelper.Exists(t.FullName)) { var properies = t.GetProperties(BindingFlags.Instance | BindingFlags.Public); var list = new List <PropertyInfo>(); foreach (var p in properies) { if (p.CanWrite && p.GetCustomAttribute <NotMappedAttribute>() == null) { list.Add(p); } } MemoryCacheHelper.SetCache(t.FullName, list.ToArray(), TimeSpan.FromDays(1)); } return(MemoryCacheHelper.GetCache <PropertyInfo[]>(t.FullName)); }
/// <summary> /// 获取用户权限 /// </summary> /// <param name="user"></param> /// <returns></returns> public async Task <List <InterfaceOperationModel> > GetUserAuthoritiesAsync(AspNetUser user) { var key = $"{user.Id}_UserAuthorities"; if (!MemoryCacheHelper.Exists(key)) { var list = new List <InterfaceOperationModel>(); var b = (await _context.QueryNumberBySqlAsync($"SELECT COUNT(b.Id) FROM AspNetRole a,AspNetUserRole b WHERE a.Id=b.RoleId AND b.UserId={user.Id} AND a.Name='{nameof(RoleTypes.Admin)}'")) > 0; string sql = GET_ALL_OPERATION_SQL; if (!b) { sql = $@"SELECT t2.InterfaceName,t2.OperationName FROM ({GET_ALL_OPERATION_SQL}) t2,DDomainAuthority t3,`aspnetusers` t4 WHERE t3.`OperationId`=t2.`Id` AND t4.`Id`= '{user.Id}' AND(t3.`AuthorityId`= t4.`AuthorityId` OR t3.`AuthorityId` IN(SELECT a.AuthorityId FROM `aspnetroles` a,`aspnetuserroles` b WHERE a.`Id`= b.`RoleId` AND b.`UserId`= '{user.Id}'))"; } list = await _context.QueryListBySqlAsync <InterfaceOperationModel>(sql); MemoryCacheHelper.SetCache(key, list); } return(MemoryCacheHelper.GetCache <List <InterfaceOperationModel> >(key)); }