http://www.jb51.net/article/20147.htm  引用

<authentication mode="Forms"><!--权限受到阻碍将会跳转到这个页面,不写找不到的时候则报404-->
<forms loginUrl="~/ExcelEport/Login" timeout="2880" />
</authentication>

// [Authorize(Roles = "Admin")]//只有通过用户才可以访问该方法
public ActionResult likJinbulai()//你要设置权限的页面
{
//判断通过身份验证的用户是否有权限访问本页面
FormsIdentity id = (FormsIdentity)HttpContext.User.Identity;
//判断通过身份验证的用户是否是Admin角色
if (id.Ticket.UserData.Contains("Admin"))
{
return View();
//跳转到访问权限不够的错误提示页面
}

else
{
ViewBag.Authorize = false;
return View("youjinbulai");
//从哪里来回哪里去,在湖区的页面添加以下代码
//@if (@ViewBag.Authorize!=null)
//{
// if (!(@ViewBag.Authorize) )
// {
// <script type="text/javascript">
// $(function () {
// alert("您没有权限");
// });
// </script>
// }
//}
}

}

///登陆用户处理Action

public ActionResult LoginHandler(string userName, string userPwd,string role)
{
//mvc
////该用户通过验证后,会得到数据库字段Roles
////验证成功直接写入客户端cookie票据
//FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
//1,
//userName,//用户登陆进来的用户名
//DateTime.Now,
//DateTime.Now.AddMinutes(20),
//true,//是否存在持久,存储在客户端
//role//登陆用户的角色写入登陆用户的角色
//);
////加密身份验证票据
//string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
////把准备好的cookie加入到响应流中
//System.Web.HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
//authCookie.Expires = authTicket.Expiration;//票据过期时间
////把准备好的cookie加入到响应流中
//System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);

//web
//Forms身份验证初始化
FormsAuthentication.Initialize();
//验证用户输入并得到登录用户,txtName是用户名称,txtPassword是登录密码
//UserModel um = ValidUser(txtName.Text.Trim(), txtPassword.Text.Trim());

//创建身份验证票据
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
userName,
DateTime.Now,
DateTime.Now.AddMinutes(30),
true,
role,//用户所属的角色字符串
FormsAuthentication.FormsCookiePath);
//加密身份验证票据
string hash = FormsAuthentication.Encrypt(ticket);
//创建要发送到客户端的cookie
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
if (ticket.IsPersistent)
{
cookie.Expires = ticket.Expiration;
}
//把准备好的cookie加入到响应流中
Response.Cookies.Add(cookie);

//转发到请求的页面
//Response.Redirect(FormsAuthentication.GetRedirectUrl(userName, false));
return View("youjinbulai");
}

//注销票据 
public ActionResult ClearTicket()
{
//注销票据
FormsAuthentication.SignOut();
string script = "alert('您已经安全退出了!');";
return JavaScript(script);
}

//以下写在Global.asax

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
//mvc
//HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
//if (authCookie == null || authCookie.Value == "")
//{
// return;
//}
//FormsAuthenticationTicket authTicket = null;
//try
//{
// authTicket = FormsAuthentication.Decrypt(authCookie.Value);
//}
//catch
//{
// return;
//}
//string[] roles = authTicket.UserData.Split(new char[] { ';' });
//if (Context.User != null)
//{
// Context.User = new System.Security.Principal.GenericPrincipal(Context.User.Identity, roles);
//}
//webform
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;

string userData = ticket.UserData;
string[] roles = userData.Split(',');
//重建HttpContext.Current.User,加入用户拥有的角色数组
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
}
}
}
}

权限验证MVC的更多相关文章

  1. ASP.NET MVC View 和 Web API 的基本权限验证

    ASP.NET MVC 5.0已经发布一段时间了,适应了一段时间,准备把原来的MVC项目重构了一遍,先把基本权限验证这块记录一下. 环境:Windows 7 Professional SP1 + Mi ...

  2. MVc Forms Membership rolemanage 角色权限验证管理

    Forms  登录验证Membership 权限验证rolemanage 角色管理 以往的项目中只有单纯的Forms 验证今天想把这三个结合到mvc 中发现要导入aspnet_ 相关表,但是有个问题验 ...

  3. Asp.net Mvc 身份验证、异常处理、权限验证(拦截器)实现代码

    本问主要介绍asp.net的身份验证机制及asp.net MVC拦截器在项目中的运用.现在让我们来模拟一个简单的流程:用户登录>权限验证>异常处理 1.用户登录 验证用户是否登录成功步骤直 ...

  4. ASP.NET MVC权限验证 封装类

    写该权限类主要目地 为了让权限配置更加的灵活,可以根据SQL.json.或者XML的方式来动态进行页面的访问控制,以及没有权限的相关跳转. 使用步骤 1.要建一个全局过滤器 //受权过滤器 publi ...

  5. Spring MVC 使用拦截器优雅地实现权限验证功能

    在上一篇 SpringAOP 实现功能权限校验功能 中虽然用AOP通过抛异常,请求转发等勉强地实现了权限验证功能,但感觉不是那么完美,应该用拦截器来实现才是最佳的,因为拦截器就是用来拦截请求的,在请求 ...

  6. MVC 自定义AuthorizeAttribute 实现权限验证

    MVC内置的AuthorizeFilter先于Action/Result过滤器执行,为网站权限验证提供了很好的一套验证机制. 通过自定义的AuthorizeAttribute可以实现对用户权限的验证. ...

  7. NET MVC权限验证

    ASP.NET MVC权限验证 封装类 写该权限类主要目地 为了让权限配置更加的灵活,可以根据SQL.json.或者XML的方式来动态进行页面的访问控制,以及没有权限的相关跳转. 使用步骤 1.要建一 ...

  8. C# MVC权限验证

    前言 之前一直没怎么接触过权限验证这块,刚好公司老平台改版,就有了这篇权限验证.此篇文章大致讲解下 精确到按钮级别的验证如何实现.以及权限验证设计的参考思路(菜鸟一枚,大神勿喷). 在开发大项目的时候 ...

  9. Web用户的身份验证及WebApi权限验证流程的设计和实现 asp.net mvc AllowAnonymous 不起作用, asp.net mvc 匿名访问

    原文地址: https://blog.csdn.net/zjlovety/article/details/17095627 前言:Web 用户的身份验证,及页面操作权限验证是B/S系统的基础功能,一个 ...

随机推荐

  1. 关于jquery-weui.js中时间控件datetimepicker的使用

    今天第一次接触jquery-weui,不太了解用法,然而官方文档写的也很简略,只好打开源代码进行研究,我想要的是设置开始日期大于当前日期,然后在源码中发现有min这个默认为undefined的属性,于 ...

  2. mysql忘记root密码或报错:ERROR 1044 (42000): Access denied for user ”@’localhost’ to database ‘xx‘

    有的时候忘记了root密码或其他用户的密码,登录的时候报错:ERROR 1044 (42000): Access denied for user ”@’localhost’ to database ' ...

  3. linux下tar.xz结尾文件的解压方法

    xz -d ***.tar.xz tar -xvf ***.tar 可以看到这个压缩包也是打包后再压缩,外面是xz压缩方式,里层是tar打包方式.

  4. Elasticsearch5 及 head插件 安装说明

    Elasticsearch5.X及 head插件 安装说明: 1.下载elasticsearch安装文件: a) 下载官方源码: https://artifacts.elastic.co/downlo ...

  5. UVA - 11475 Extend to Palindrome —— 字符串哈希 or KMP or 后缀数组

    题目链接:https://vjudge.net/problem/UVA-11475 题意: 给出一个字符串,问在该字符串后面至少添加几个字符,使得其成为回文串,并输出该回文串. 题解: 实际上是求该字 ...

  6. webpack为什么加载不了css?

    原文地址: https://segmentfault.com/q/1010000005099261 这个app是用react写的. webpack的loader设置是这样的 module:{ load ...

  7. c语言学习的第12天

    #include <stdio.h> int main(void) { int *p; int i=5; char ch='A'; p=&i; *p=99; printf(&quo ...

  8. blog真正的首页

    声明:此Django分类下的教程是追梦人物所有,地址http://www.jianshu.com/u/f0c09f959299,本人写在此只是为了巩固复习使用 上一节我们阐明了django的开发流程, ...

  9. 2048聚合版开源代码,cocos2d-js编写,基于CocosEditor开发工具,可运行Android,ios,html5等

    1. [代码][JavaScript]代码         /** * @GameName : * 2048 * * @DevelopTool: * Cocos2d-x Editor (CocosEd ...

  10. BZOJ 1640 [Usaco2007 Nov]Best Cow Line 队列变换:贪心【字典序最小】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1640 题意: 给你一个长度为n的字符串. 你可以将原串的首字母或尾字母移动到新串的末尾. ...