4.4 权限属性RequireAuthorizationAttribute

[csharp] view plaincopy

 
  1. "font-size:14px;">///
  2. /// 权限验证属性类
  3. ///
  4. public class RequireAuthorizeAttribute : AuthorizeAttribute
  5. {
  6. ///
  7. /// 用户权限列表
  8. ///
  9. public UserAuthModel[] UserAuthList
  10. {
  11. get
  12. {
  13. return AuthorizedUser.Current.UserAuthList;
  14. }
  15. }
  16. ///
  17. /// 登录用户票据
  18. ///
  19. public string UserLoginTicket
  20. {
  21. get
  22. {
  23. return AuthorizedUser.Current.UserLoginTicket;
  24. }
  25. }
  26. public override void OnAuthorization(AuthorizationContext filterContext)
  27. {
  28. base.OnAuthorization(filterContext);
  29. ////验证是否是登录用户
  30. var identity = filterContext.HttpContext.User.Identity;
  31. if (identity.IsAuthenticated)
  32. {
  33. var actionName = filterContext.ActionDescriptor.ActionName;
  34. var controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
  35. //验证用户操作是否在权限列表中
  36. if (HasActionQulification(actionName, controllerName, identity.Name))
  37. if (!string.IsNullOrEmpty(UserLoginTicket))
  38. //有效登录用户,有权限访问此Action,则写入Cookie信息
  39. filterContext.HttpContext.Response.Cookies[FormsAuthentication.FormsCookieName].Value = UserLoginTicket;
  40. else
  41. //用户的Session, Cookie都过期,需要重新登录
  42. filterContext.HttpContext.Response.Redirect("~/Account/Login", false);
  43. else
  44. //虽然是登录用户,但没有该Action的权限,跳转到“未授权访问”页面
  45. filterContext.HttpContext.Response.Redirect("~/Home/UnAuthorized", true);
  46. }
  47. else
  48. {
  49. //未登录用户,则判断是否是匿名访问
  50. var attr = filterContext.ActionDescriptor.GetCustomAttributes(true).OfType();
  51. bool isAnonymous = attr.Any(a => a is AllowAnonymousAttribute);
  52. if (!isAnonymous)
  53. //未验证(登录)的用户, 而且是非匿名访问,则转向登录页面
  54. filterContext.HttpContext.Response.Redirect("~/Account/Login", true);
  55. }
  56. }
  57. ///
  58. /// 从权限列表验证用户是否有权访问Action
  59. ///
  60. ///
  61. ///
  62. ///
  63. private bool HasActionQulification(string actionName, string controllerName, stringuserName)
  64. {
  65. //从该用户的权限数据列表中查找是否有当前Controller和Action的item
  66. var auth = UserAuthList.FirstOrDefault(a =>
  67. {
  68. bool rightAction = false;
  69. bool rightController = a.Controller == controllerName;
  70. if (rightController)
  71. {
  72. string[] actions = a.Actions.Split(',');
  73. rightAction = actions.Contains(actionName);
  74. }
  75. return rightAction;
  76. });
  77. //此处可以校验用户的其它权限条件
  78. //var notAllowed = HasOtherLimition(userName);
  79. //var result = (auth != null) && notAllowed;
  80. //return result;
  81. return (auth != null);
  82. }
  83. }

4.5 业务Controller示例

[csharp] view plaincopy

 
    1. "font-size:14px;">public class ProductController : WebControllerBase
    2. {
    3. [AllowAnonymous]
    4. public ActionResult Query()
    5. {
    6. return View("ProductQuery");
    7. }
    8. [HttpGet]
    9. //[AllowAnonymous]
    10. [RequireAuthorize]
    11. public ActionResult Detail(string id)
    12. {
    13. var cookie = HttpContext.Request.Cookies;
    14. string url = base.ApiUrl + "/Get/" + id;
    15. HttpClient httpClient = HttpClientHelper.Create(url, base.UserLoginTicket);
    16. string result = httpClient.GetString();
    17. var model = JsonSerializer.DeserializeFromString(result);
    18. ViewData["PRODUCT_ADD_OR_EDIT"] = "E";
    19. return View("ProductForm", model);
    20. }
    21. }

Web用户的身份验证及WebApi权限验证流程的设计和实现(续)的更多相关文章

  1. [置顶] Web用户的身份验证及WebApi权限验证流程的设计和实现 (不是Token驗證!!!不是Token驗證!!!都是基於用户身份的票据信息驗證!!!)

     转发 http://blog.csdn.net/besley/article/details/8516894 不是Token驗證!!!不是Token驗證!!!都是基於用户身份的票据信息驗證!!! [ ...

  2. 转 Web用户的身份验证及WebApi权限验证流程的设计和实现

    前言:Web 用户的身份验证,及页面操作权限验证是B/S系统的基础功能,一个功能复杂的业务应用系统,通过角色授权来控制用户访问,本文通过Form认证,Mvc的Controller基类及Action的权 ...

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

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

  4. Web用户的身份验证及WebApi权限验证流程的设计和实现

    5. WebApi 服务端代码示例 5.1 控制器基类ApiControllerBase [csharp] view plaincopy   /// /// Controller的基类,用于实现适合业 ...

  5. Web用户的身份验证及WebApi权限验证流程的设计和实现(尾)

    5. WebApi 服务端代码示例 5.1 控制器基类ApiControllerBase [csharp] view plaincopy   /// /// Controller的基类,用于实现适合业 ...

  6. SpringAOP01 利用AOP实现权限验证、利用权限验证服务实现权限验证

    1 编程范式 1.1 面向过程 1.2 面向对象 1.3 面向切面编程 1.4 函数式编程 1.5 事件驱动编程 2 什么是面向切面编程 2.1 是一种编程范式,而不是一种编程语言 2.2 解决一些特 ...

  7. 学习总结之 WebApi 用户登录和匿名登录,及权限验证

    近些天,看了一些博客园大牛关于webApi项目的的文章,也有请教师兄一些问题,自己做了个Demo试了试,收获甚多.感谢感谢,下面是我一些学习的总结,如若有错的地方请多多指教!! WebApi登陆与身份 ...

  8. 简单两步快速实现shiro的配置和使用,包含登录验证、角色验证、权限验证以及shiro登录注销流程(基于spring的方式,使用maven构建)

    前言: shiro因为其简单.可靠.实现方便而成为现在最常用的安全框架,那么这篇文章除了会用简洁明了的方式讲一下基于spring的shiro详细配置和登录注销功能使用之外,也会根据惯例在文章最后总结一 ...

  9. [Abp 源码分析]十二、多租户体系与权限验证

    0.简介 承接上篇文章我们会在这篇文章详细解说一下 Abp 是如何结合 IPermissionChecker 与 IFeatureChecker 来实现一个完整的多租户系统的权限校验的. 1.多租户的 ...

随机推荐

  1. 1.2.3 创建Cocos2D-iPhone的帮助文档

    http://book.51cto.com/art/201303/383957.htm <Cocos2D权威指南>第1章开始前的准备工作,本章我们将介绍什么是Cocos2D以及有关Coco ...

  2. nginx 根据POST GET方法跳转

    location ~ /server/ {    proxy_pass_header   Server;    proxy_set_header Host $http_host;    proxy_r ...

  3. 免格式化制作老毛桃PE工具

    由于移动硬盘数据很多,格式化制作太麻烦 先去老毛桃官网下载PE,生成ISO文件 将移动硬盘单独划分一个2G的空间用于装老毛桃,并格式化为FAT32格式,这样就避免全盘格式化了,只需要格式化这个分区   ...

  4. python 错误 error: invalid command 'egg_info'

    Processing /bs4-0.0.1/setuptools-38.4.0/numpy-1.14.0    Complete output from command python setup.py ...

  5. (2)apache安装、配置及使用

    一.apache安装 假设我们我们的安装路径是 C:\Apache2.2 运行软件 如果是本地学习使用 第二行服务名localhost 其他随便 一个自动安装,一个自定义安装,没有什么特别重要的,一直 ...

  6. APP专项测试 | 内存及cpu

    命令: adb shell dumpsys meminfo  packagename 关注点: 1.Native/Dalvik 的 Heap 信息 具体在上面的第一行和第二行,它分别给出的是JNI层和 ...

  7. 线段树【SP1043】GSS1 - Can you answer these queries I

    Description 给出了序列\(A_1,A_2,-,A_n\). \(a_i \leq 15007,1 \leq n \leq 50000\).查询定义如下: 查询\((x,y)=max{a_i ...

  8. Tarjan缩点【p1726】上白泽慧音

    Description 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的 ...

  9. 分层图【p4822】[BJWC2012]冻结

    Description "我要成为魔法少女!" "那么,以灵魂为代价,你希望得到什么?" "我要将有关魔法和奇迹的一切,封印于卡片之中„„" ...

  10. 集群/分布式/微服务/SOA 转

    https://www.cnblogs.com/Java3y/p/9479410.html 二.集群/分布式/微服务/SOA是什么? 像我这种技术小白,看到这些词(集群/分布式/微服务/SOA)的时候 ...