/// <summary> /// 校验登入用户是否拥有页面权限 /// 创建 毛枫 2015-4-21 /// </summary> /// <param name="viewPower"></param> /// <returns></returns> public static bool CompareRole(string viewPower) { //HttpContext context = HttpContext.Current; List <T_POWERS> adminPowersList = new List <T_POWERS>(); bool result = false; int userId = 0; string userName = string.Empty; if (HttpContext.Current.Session["A_Power"] == null) { //如果A_Power的Session是为空的话就首先判断当前用户是否登录 if (IsLogIn(ref userId, ref userName)) { AdminUserModel user = GetCurrentAdminUser(); string A_Code = user.A_CODE; //获取当前用户角色CODE List <string> userRoleList = GetUserRole(A_Code); foreach (string q in userRoleList) { //获取角色对应权限 adminPowersList.AddRange(GetUserPower(q)); } //放入Session中 } else { return(false); } } else { //存在Session string powerList = WebHelperUtil.SessionGet("A_Power"); adminPowersList = powerList.toJsonObject <List <T_POWERS> >(); } if (adminPowersList != null) { //将权限Model放入SESSION中 string powerList = adminPowersList.toJson(); WebHelperUtil.SessionAdd("A_Power", powerList, 60); } //判断登入用户有无页面权限 if (adminPowersList.Any(qq => qq.P_NAME == viewPower)) { result = true; } return(result); }
/// <summary> /// 管理员登录 /// 创建 毛枫 2015-4-21 /// 修改 毛枫 2015-7-31 /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <returns></returns> public static bool AdminLoginIn(string username, string password) { bool result = false; try { T_ADMIN admin = new T_ADMIN(); admin = IsLoginFromSql(username, password); if (admin != null) { #region Cookie和Session的设置 //System.Web.HttpContext.Current.Session["A_ID"] = admin.ID; //System.Web.HttpContext.Current.Session["A_Code"] = admin.A_CODE; //System.Web.HttpContext.Current.Session["A_TrueName"] = admin.A_TRUE_NAME; //System.Web.HttpContext.Current.Session["A_AdminName"] = admin.A_NAME; //System.Web.HttpContext.Current.Session.Timeout = 10800; WebHelperUtil.SessionAdd("A_ID", admin.ID.ToString(), 60); WebHelperUtil.SessionAdd("A_Code", admin.A_CODE, 60); WebHelperUtil.SessionAdd("A_TrueName", admin.A_TRUE_NAME, 60); WebHelperUtil.SessionAdd("A_AdminName", admin.A_NAME, 60); ////生成用户模型 //System.Web.HttpContext.Current.Session["A_AdminUser"] = new AdminUserModel() //{ // A_ID = admin.ID, // A_CODE = admin.A_CODE, // A_NAME = admin.A_NAME, // A_CHINESE_NAME = admin.A_TRUE_NAME, //}; string userModle = new AdminUserModel() { A_ID = admin.ID, A_CODE = admin.A_CODE, A_NAME = admin.A_NAME, A_CHINESE_NAME = admin.A_TRUE_NAME, }.toJson(); //加密处理 userModle = EncryptUtil.Base64Encode(userModle); WebHelperUtil.SetCookie("A_AdminUser", userModle, ExpiresDayCookiesSession); //生成验证字符串cookie string authStr = admin.ID + "^" + username.ToLower() + "^" + DateTime.Now.AddHours(2); authStr = EncryptUtil.DESEncryptString(authStr); //添加Cookie WebHelperUtil.SetCookie("AdminToken", EncryptUtil.MD5(admin.ID.ToString(), 16), ExpiresDayCookiesSession); WebHelperUtil.SetCookie("AdminAuth", authStr, ExpiresDayCookiesSession); WebHelperUtil.SetCookie("AdminLastLogTime", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), ExpiresDayCookiesSession); WebHelperUtil.SetCookie("AdminUserLogin", "login", ExpiresDayCookiesSession); #endregion LoginCommon.InsertAdminLoginLog(admin); result = true; } else { result = false; } } catch (Exception ex) { result = false; } return(result); }