.net MVC 登陆模块后台代码
首先是拦截器
public class AuthLoginAttribute : ActionFilterAttribute
{
public bool IsLogin = true;
/// <summary>
/// 登录状态
/// </summary>
public AuthLoginAttribute()
{
IsLogin = true;
} /// <summary>
/// 登录状态
/// </summary>
/// <param name="islogin"></param>
public AuthLoginAttribute(bool islogin)
{
IsLogin = islogin;
} /// <summary>
/// 判断登录状态
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//排除例外
if (!IsLogin)
return;
string loginUrl = "/Home/Login";
//上一次请求地址
string refUrl = filterContext.HttpContext.Request.UrlReferrer != null ? filterContext.HttpContext.Request.UrlReferrer.ToString() : loginUrl;
//控制器
string controlName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName.ToLower();
//方法
string actionName = filterContext.ActionDescriptor.ActionName.ToLower();
//子方法
bool isChildAction = filterContext.IsChildAction;
//是否为异步请求
bool isAjax = filterContext.HttpContext.Request.IsAjaxRequest();
UserBaseController controller = filterContext.Controller as UserBaseController;
if (!controller.IsLogin)
{
//异步处理
if (isAjax)
{
//这里可以添加一些过滤登录的异步操作如:公共上传图片
JsonResult jr = new JsonResult();
jr.Data = new BaseResponse<object>
{
ErrorCode = ,
Message = "请先登录!",
Data = "need login"
};
filterContext.Result = jr;
}
else if (filterContext.IsChildAction)
{
filterContext.Result = new ContentResult() { Content = "请先登录!" };
}
else
{
string pq = null;
if (filterContext.HttpContext.Request.Url != null)
{
pq = filterContext.HttpContext.Request.Url.PathAndQuery;
}
filterContext.Result = new RedirectResult(loginUrl);
}
}
else
{
//权限判断
var userAuthority = OperSession.UserAuthority;
string noAuth = "/Home/NoAuthorityUser?back=" + HttpUtility.UrlEncode(refUrl);
if (userAuthority == null || userAuthority.Count == )
{
filterContext.Result = new RedirectResult(noAuth);
}
else
{
string route = "/" + controlName + "/" + actionName;
//排除首页登陆,异步
if (route == "/home/index" || route == "/home/login" || isAjax || route == "/admin/userprofile" || route == "/admin/index") return;
//进行检测 是否有可访问的权限
if (!userAuthority.Exists(a => a.Action?.ToLower() == actionName && a.Controller?.ToLower() == controlName))
{
filterContext.Result = new RedirectResult(noAuth);
} }
}
}
}
登陆验证代码
/// <summary>
/// 用户登录
/// </summary>
/// <param name="uName"></param>
/// <param name="uPwd"></param>
/// <param name="uIP">客户端IP</param>
/// <param name="sessionID">sessionID</param>
/// <param name="isMD5">是否MD5加密</param>
/// <returns></returns>
public LoginResult ValidateLogin(string uName, string uPwd, string uIP, string sessionID, bool isMD5 = true)
{
string pwdMd5 = uPwd;
if (isMD5)
{
pwdMd5 = uPwd.Crypt_MD5_Encode();
}
VUser loginUser = GetUser(uName, pwdMd5);
if (loginUser == null)
{
return new LoginResult()
{
Message = "账号或密码错误。",
ResultType =
};
}
if (!loginUser.IsEnable)
{
return new LoginResult()
{
Message = "账号已禁用,请联系管理员。",
ResultType =
};
}
//用户权限初始化
var urCatalogue = loginUser.Permissions.IsNullOrEmpty()?new List<VPermission>():PermissionBll.GetIntence().GetUserCatalog(loginUser.Permissions);
if (urCatalogue.Count > )
{
//IList<Dictionary<int, IEnumerable<SysRoleExtensionInfo>>> menuData = new IList<Dictionary<int, IEnumerable<SysRoleExtensionInfo>>>();
//var pMenu = urCatalogue.Where(a => a.ParentID == 0);
//foreach (var pItem in pMenu)
//{
// var cMenu = urCatalogue.Where(a => a.ParentID == pItem.ID);
// foreach (var cItem in cMenu)
// {
// var ccMenu = urCatalogue.Where(a => a.ParentID == cItem.ID);
// foreach (var ccItem in ccMenu)
// {
// menuData.Add(pItem.CatalogueID, urCatalogue.Where(a => a.ParentID == pItem.ID));
// }
// }
//}
loginUser.ProjPermissions = loginUser.DataPermissions.IsNullOrEmpty()?new List<DataPermission>():loginUser.DataPermissions.ToObjectFromJson<List<DataPermission>>();
//权限记录
OperSession.UserAuthority = urCatalogue.ToList();
//记录登录用户信息
loginUser.LoginSessionID = sessionID;
loginUser.LoginIP = uIP;
Helper.OperSession.UserInfo = loginUser;
return new LoginResult()
{
Message = "登录成功。",
ResultType =
};
}
return new LoginResult()
{
Message = "该登录用户没有权限。",
ResultType =
};
}
public class OperSession
{
/// <summary>
/// 后台操作员登录信息
/// </summary>
public static VUser UserInfo
{
get
{
if (HttpContext.Current.Session[ConstVar.UserSessionKey] != null)
{
return HttpContext.Current.Session[ConstVar.UserSessionKey] as VUser;
}
return null;
}
set
{
HttpContext.Current.Session[ConstVar.UserSessionKey] = value;
}
} /// <summary>
/// 用户权限
/// </summary>
public static List<VPermission> UserAuthority
{
get
{
if (HttpContext.Current.Session[ConstVar.UserAuthorityKey] != null)
{
return HttpContext.Current.Session[ConstVar.UserAuthorityKey] as List<VPermission>;
}
return null;
}
set
{
HttpContext.Current.Session[ConstVar.UserAuthorityKey] = value;
}
}
}
public class BaseResponse
{
public bool Success { get; set; } public int ErrorCode { get; set; } public string Message { get; set; } public object Data { get; set; }
}
.net MVC 登陆模块后台代码的更多相关文章
- mvc 登陆界面+后台代码
上代码 前端+js(懒得分文件了) @{ ViewBag.Title = "MVC权限系统架构学习-登录"; Layout = "/Views/Shared/_LoadJ ...
- ASP.NET -- WebForm -- Cookie的使用 应用程序权限设计 权限设计文章汇总 asp.net后台管理系统-登陆模块-是否自动登陆 C# 读写文件摘要
ASP.NET -- WebForm -- Cookie的使用 ASP.NET -- WebForm -- Cookie的使用 Cookie是存在浏览器内存或磁盘上. 1. Test3.aspx文件 ...
- php截取后台登陆密码的代码
php截取后台登陆密码的代码,很多时候黑客留了这样的代码,大家一定要注意下if($_POST[loginsubmit]!=){ //判断是否点了登陆按钮 $sb=user:.$_POST[userna ...
- ASP.NET MVC搭建项目后台UI框架—2、菜单特效
目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NE ...
- Spring py登陆模块(包含 记录登陆时间,记录ip,增加积分)
嘛基于最近的复习准备写个关于spring登陆模块的小程序 虽然小但是五脏俱全呐 话不多说让我来介绍一下今天的登陆程序. 这些是 基于Spring JDBC 的持久层实现 基于Spring 声明事物的业 ...
- 微信小程序 人脸识别登陆模块
微信小程序---人脸识别登陆的实现 关键词:微信小程序 人脸识别 百度云接口 前言 这是一篇关于一个原创微信小程序开发过程的原创文章.涉及到的核心技术是微信小程序开发方法和百度云人脸识别接口.小程序的 ...
- 基于AOP的MVC拦截异常让代码更优美
与asp.net 打交道很多年,如今天微软的优秀框架越来越多,其中微软在基于mvc的思想架构,也推出了自己的一套asp.net mvc 框架,如果你亲身体验过它,会情不自禁的说‘漂亮’.回过头来,‘漂 ...
- ASP.NET MVC搭建项目后台UI框架—1、后台主框架
目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NE ...
- ASP.NET MVC搭建项目后台UI框架—11、自动加载下拉框查询
ASP.NET MVC搭建项目后台UI框架—1.后台主框架 需求:在查询记录的时候,输入第一个字,就自动把以这个字开头的相关记录查找出来,输入2个字就过滤以这两个子开头的记录,依次类推. 突然要用到这 ...
随机推荐
- bzoj 3531 [Sdoi2014]旅行 (树剖+线段树 动态开点)
3531: [Sdoi2014]旅行 Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 2984 Solved: 1312[Submit][Status ...
- MySQL的group_concat()函数合并多行数据
一个很有用的函数 group_concat(),手册上说明:该函数返回带有来自一个组的连接的非NULL值的字符串结果. 通俗点理解,其实是这样的:group_concat()会计算哪些行属于同一组,将 ...
- matlab calibration toolbox -- matlab标定工具的使用方法--去畸变和双目校正
matlab calibration toolbox -- matlab标定工具的使用方法--去畸变和双目校正 2015-04-06 22:45 5407人阅读 评论(2) 收藏 举报 分类: 机器 ...
- bzoj2817[ZJOI2012]波浪
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=2817 波浪 [问题描述] 阿米巴和小强是好朋友. 阿米巴和小强在大海旁边看海水的波涛.小 ...
- Python之旅:并发编程之协程
一 引子 本节的主题是基于单线程来实现并发,即只用一个主线程(很明显可利用的cpu只有一个)情况下实现并发,为此我们需要先回顾下并发的本质:切换+保存状态 cpu正在运行一个任务,会在两种情况下切走去 ...
- word绘图画布
这样图形组合不会随着位置的变动而出现相对变化
- Spring中Model,ModelMap以及ModelAndView之间的区别
原文链接:http://blog.csdn.net/zhangxing52077/article/details/75193948 Spring中Model,ModelMap以及ModelAndVie ...
- vue.js初识(一)
vue.js安装 官网:http://cn.vuejs.org/ 官方安装介绍:http://cn.vuejs.org/v2/guide/installation.html MVVM框架:View.V ...
- 2018年5月6日GDCPC (广东赛区)总结
试机是队友浩哥一个人去的,因为觉得华工去了不少次了,环境也比较熟悉了.直到看到了现场环境,感觉有些拥挤,不如从前那样宽敞,增加了一些紧张的不适感. 比赛开始时,我们三人分头读题,虽说题目比较简短,但第 ...
- bzoj千题计划187:bzoj1770: [Usaco2009 Nov]lights 燈 (高斯消元解异或方程组+枚举自由元)
http://www.lydsy.com/JudgeOnline/problem.php?id=1770 a[i][j] 表示i对j有影响 高斯消元解异或方程组 然后dfs枚举自由元确定最优解 #in ...