public ActionResult <DataResult> Login(string account, string password) { DataResult dataResult = new DataResult(); if (string.IsNullOrWhiteSpace(account) || string.IsNullOrWhiteSpace(password)) { dataResult.msg = "账号和密码不能为空"; dataResult.code = "201"; return(dataResult); } AdminBLL adminBLL = new AdminBLL(); AdminEntity adminEntity = adminBLL.GetAccountAndPassword(account, Helper.DataEncrypt.DataMd5(password)); if (adminEntity == null) { dataResult.msg = "账号或密码错误"; dataResult.code = "201"; return(dataResult); } if (adminEntity.forbidden) { dataResult.msg = "账号已被禁用"; dataResult.code = "201"; return(dataResult); } LoginResult loginResult = new LoginResult(); loginResult.adminEntity = adminEntity; AdminTokenBLL adminTokenBLL = new AdminTokenBLL(); AdminTokenEntity adminTokenEntity = adminTokenBLL.GetByAdminId(adminEntity.adminId); AdminTokenEntity adminToken = new AdminTokenEntity(); if (adminTokenEntity == null) { adminToken = adminTokenBLL.Create(adminEntity.adminId); } else { adminToken = adminTokenBLL.Update(adminTokenEntity); } loginResult.token = adminToken.token; dataResult.data = loginResult; dataResult.code = "200"; return(dataResult); }
/// <summary> /// 根据token获取个人信息 /// </summary> /// <param name="token"></param> /// <returns></returns> protected AdminEntity GetAdminByToken(string token) { if (!string.IsNullOrWhiteSpace(token)) { AdminBLL adminBLL = new AdminBLL(); AdminTokenBLL adminTokenBLL = new AdminTokenBLL(); AdminTokenEntity adminTokenEntity = adminTokenBLL.GetByToken(token); AdminEntity adminEntity = adminBLL.GetById(adminTokenEntity.adminId); return(adminEntity); } else { return(null); } }
/// <summary> /// 重写,登录过滤 /// </summary> /// <param name="filterContext"></param> public override void OnActionExecuting(ActionExecutingContext filterContext) { //获取跳过验证的标签如果有则跳过验证 var isDefined = false; var controllerActionDescriptor = filterContext.ActionDescriptor as ControllerActionDescriptor; if (controllerActionDescriptor != null) { isDefined = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true) .Any(a => a.GetType().Equals(typeof(SkipCheckLoginAttribute))); } if (isDefined) { return; } DataResult dr = new DataResult(); if (filterContext.ActionArguments.Count < 1) { dr.code = "300"; dr.msg = "参数是必需的"; filterContext.Result = new JsonResult(dr); return; } dynamic pars = filterContext.ActionArguments; if (!pars.ContainsKey("token")) { dr.code = "300"; dr.msg = "未包含token"; filterContext.Result = new JsonResult(dr); return; } // 获取token string token = pars["token"]; if (string.IsNullOrWhiteSpace(token)) { dr.msg = "token为空!"; filterContext.Result = new JsonResult(dr); return; } AdminTokenBLL adminTokenBLL = new AdminTokenBLL(); AdminTokenEntity adminTokenEntity = adminTokenBLL.GetByToken(token); if (adminTokenEntity == null) { dr.msg = "token错误!"; filterContext.Result = new JsonResult(dr); return; } int difference = 10000; if (DateTime.Now.Subtract(adminTokenEntity.createDate).Minutes > difference) { dr.msg = "token过期!"; filterContext.Result = new JsonResult(dr); return; } adminTokenBLL.UpdateTime(adminTokenEntity); base.OnActionExecuting(filterContext); }