MVC-登录并设置角色
1、新建一个类,设置角色:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Security; namespace InfoData.Enum
{
public enum ERoles
{
Admin,
User
} public class ERolesHelper
{
public static string GetValue(ERoles role)
{
return role == ERoles.Admin ? "Admin" : "User";
}
} public class ERolesAttribute : AuthorizeAttribute
{
//重载此方法,模拟自定义的角色授权机制
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
//获得当前的验证cookie
HttpCookie authCookie = httpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie == null || authCookie.Value == "")
{
return false;
}
else
{
FormsAuthenticationTicket authTicket;
try
{
//对当前的cookie进行解密
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
if (authTicket != null)
{
//和存入时的分隔符有关系,此处存入时的分隔符为逗号
var userRoles = authTicket.UserData.Split(new[] { ',' }).ToList();
var roles = Roles.Split(new[] { ',' }).ToList();
return roles.Any(x => userRoles.Contains(x));
}
else
{
return false;
}
}
catch
{
return false;
}
}
} //没授权时跳到页面
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
filterContext.Result = new RedirectResult("/UserManage/Admin/Login"); ;
}
}
}
2、登录代码:
DateTime now = DateTime.Now;
string roles = ERolesHelper.GetValue(ERoles.Admin);
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(, userName, now, now.AddMinutes(), false, roles);//写入用户角色
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);//对authTicket进行加密
SessionCookieHelper.SetCookie(FormsAuthentication.FormsCookieName, encryptedTicket, now.AddMinutes());
3、退出代码:
FormsAuthentication.SignOut();
SessionCookieHelper.ClearCookie(this.AdminCookieName);
4、Action或Controller添加:[ERolesAttribute(Roles = "Admin")]
如果允许匿名访问,添加:[AllowAnonymous]
[ERolesAttribute(Roles = "Admin")]
public class AdminController : Controller
{
[AllowAnonymous]
public ActionResult Login()
{
ViewBag.aaa = EncryptionHelper.DesEncrypt("xiaoming");
Dataprovider.Sys_AdminDAO.Logout();
return View();
} [AllowAnonymous]
[HttpPost]
public ActionResult Login(Admin_Login info)
{
if (ModelState.IsValid)
{
Dataprovider.Sys_AdminDAO.Login(info.UserName, info.Password);
}
return View();
} public ActionResult AdminList()
{
return View();
}
}
MVC-登录并设置角色的更多相关文章
- 给Asp.net MVC Forms 验证设置角色访问控制
当我们使用Asp.net MVC Forms方式验证用户, 然后设置Controller 或 Action 的 Authorize属性时, 默认情况下只有Users属性可以设置(这里的Users通常是 ...
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(23)-设置角色遗留问题和为权限设置角色以及EasyUI Tabs的使用
ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1):框架搭建 (2):数据库访问层的设计Demo (3):面向接口编程 (4 ):业务逻辑层的封装 ...
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(22)-为用户设置角色
ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1):框架搭建 (2):数据库访问层的设计Demo (3):面向接口编程 (4 ):业务逻辑层的封装 ...
- 设置角色遗留问题和为权限设置角色以及EasyUI Tabs的使用
设置角色遗留问题和为权限设置角色以及EasyUI Tabs的使用 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1):框架搭建 (2):数据库访问层的设计Demo ...
- uniapp根据登录用户的角色动态的改变tabBar的数量和内容
此文章借鉴于https://blog.csdn.net/fuyuumiai/article/details/109746357,在此基础上修改小部分内容,适用于我这种uniapp小白 介绍: 现在我们 ...
- MVC中如何设置路由指定默认页
MVC中怎么设置默认页,在webform中 只要右键设置起始页就可以,但MVC中却没有这个功能,其实MVC更简单 如下: Login是控制器,Index 是动作 在全局Global.asax中改动下即 ...
- Spring mvc登录拦截器
自己实现的第一个Spring mvc登录拦截器 题目要求:拒绝未登录用户进入系统,只要发现用户未登录,则将用户请求转发到/login.do要求用户登录 实现步骤: 1.在spring的配置文件中添加登 ...
- MVC的路由设置【转】
转,MVC的路由设置. 后端获取路由里面action的参数,函数需要设置相同的参数名称才行. routes.MapRoute( "Default", "{controll ...
- OA之为用户设置角色和为用户设置权限
1.为用户设置角色 { Layout = null; } @using OA.Model <!DOCTYPE html> <html> <head> <met ...
随机推荐
- ios 通过代码调节屏幕亮度
方法: [[UIScreen mainScreen] setBrightness: value]; value:value就是屏幕的亮度值 这个值介于0和1之间 另外 这个方法 会即时刷新 无需 ...
- Arp欺骗攻击的另类应用之屌丝泡妞记
http://www.2cto.com/Article/201210/163974.html arp欺骗,我想大家都应该知道怎么回事了.不知道的去问度娘... 就不废话了,还是直接上图上教程比 ...
- Pester: Unit Testing for PowerShell
Pester is a BDD inspired testing framework for PowerShell just like Jasmine is on the JavaScript sid ...
- ADO.NET通用数据库访问类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- (转载)研究openvswitch的流量模式
最近又开始弄openvswitch,网上也有不少的资料,但是发觉都集中在openvswitch安装及简单使用或者一些原码分析,从心里来讲,感觉这些和心里得到的差距有点大啊,其实更希望能类似资料在ope ...
- ASP 生成带日期的随机数
<% Function getRnd() '**************************************** '返回值:如getRnd(),即输出2008082415534646 ...
- ASP.NET Web Service如何工作(2)
ASP.NET Web Service如何工作(2) [日期:2003-06-26] 来源:CSDN 作者:sunnyzhao(翻译) [字体:大 中 小] HTTP管道一旦调用了.asmx句柄,便 ...
- WebService简单介绍
什么是Web Services 一直没有一个明确的答案,最近听了一个大牛讲WebService,顿时明了了,作个记录免得以后忘了. N年之前,各公司都在炒作SOA,不过现在已经没人再提了,现在都改叫W ...
- XIB 不能拖线的低级错误
问题描述 创建 XIB 文件后,与其相关联的.m文件进行拖线,但无论如何都无法拖线,过程无任何反应.清除缓存重启 Xcode 均无法解决. 解决方法 事实上这是个低级错误,原因为 xib 的 view ...
- 【html】【17】高级篇--loading加载
参考: http://aspx.sc.chinaz.com/query.aspx?keyword=%E5%8A%A0%E8%BD%BD&classID=835 下载: http://sc. ...