Asp.net Controller中View和Action方法认证Authorize

在建立Web 站点安全性时

1、登录后才可访问系统文件 ——限制 Forms认证

<authentication mode="Forms">
<forms timeout="800" loginUrl="/Login/Index" path="/" protection="All" name="FinancialServicep.Web" slidingExpiration="true" />
</authentication>

2、增加Controller 、Action级别角色、权限、用户认证,下面描述一下角色认证

一、首先在登录时将 角色及相关信息存储如下代码,这里因为系统中需要客户信息较多 所以存储较多 ,仅关心roleCode及可。

  // 设置 FormsAuthentication认证
  FormsAuthentication.SetAuthCookie(loginM.LoginId + "#" + loginM.UserName + "#" + loginM.UserId + "#" + loginM.OrgId + "#" + loginM.OrgType + "#" + loginM.IsSensitive+"#"+ roleCode, false);

二、增加AuthorizeAttribute扩展认证  过滤器。

  

  public class CheckAuthentication : AuthorizeAttribute
{
public string[] checkRoles;
public override void OnAuthorization(AuthorizationContext httpContext)
{
// 获取请求该方法所需角色
checkRoles = Roles.Split(','); string controllerName = httpContext.ActionDescriptor.ControllerDescriptor.ControllerName;
string actionName = httpContext.ActionDescriptor.ActionName; base.OnAuthorization(httpContext);
} protected override bool AuthorizeCore(HttpContextBase httpContext)
{ if (httpContext == null)
{
throw new ArgumentNullException("HttpContext");
}
if (!httpContext.User.Identity.IsAuthenticated)
{
return false;
}
if (Roles == null)
{
return true;
}
if (Roles.Length == )
{
return true;
}
if (checkRoles.Any(httpContext.User.IsInRole))
{
return true;
} foreach (var item in checkRoles)
{ if (WebContext.RoleCode == item)
{
return true;
}
}
return false;
} /// <summary>
/// 没有权限用户跳转到登录
/// </summary>
/// <param name="filterContext"></param>
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
base.HandleUnauthorizedRequest(filterContext);
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
else
{
filterContext.HttpContext.Response.Redirect("/Login/Index");
}
} }
}

三、Controller 或Action 加角色限制调用过滤器及可。

        // GET: FinancingLoan
[CheckAuthentication(Roles = "Manager,Inv")]
public ActionResult Index()
{
ViewBag.MenuCode = "Menu0018";
return View();
}

这样做提高了系统访问安全性,Forms认证 限制了未登录用户不可访问网站各功能页,结合CheckAuthentication 过滤器认证 限制了没角相关功能访问角色的用户不可访问。

如有问题及时沟通讨论在评论区,也可加QQ:626382542

Asp.net Controller中View 和Action方法认证Authorize 及对AuthorizeAttribute扩展的更多相关文章

  1. Asp.net mvc 中View的呈现(一)

    [toc] 我们知道针对客户端的请求,最终都会转换为对 Controller 中的一个 Action 方法的调用,指定的 Action 方法会返回一个 ActionResult 类型的实例来响应该请求 ...

  2. ASP.NET MVC 中 View 的设计

    1. 前言  感觉有好长时间没有接触View 了,周末闲来无事,翻翻书桌上的书来回顾回顾ASP.NET MVC中View的相关内容. 2. View概述  View 通过应用程序在Action 中返回 ...

  3. ASP.NET Web API 框架研究 Action方法介绍

    在根据请求解析出匹配的Controller类型并创建实例后,要在该Controller类型中的众多Action方法中选择与请求匹配的那一个,并执行,然后返回响应. Action方法,其元数据,主要包括 ...

  4. asp.net mvc 3.0 知识点整理 ----- (2).Controller中几种Action返回类型对比

    通过学习,我们可以发现,在Controller中提供了很多不同的Action返回类型.那么具体他们是有什么作用呢?它们的用法和区别是什么呢?通过资料书上的介绍和网上资料的查询,这里就来给大家列举和大致 ...

  5. 一步步学习ASP.NET MVC3 (5)——View从Action中获得数据

    请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,我们把Razor的模板技术给大家介绍了一下,当然模板中还有其他的知识点,这个以后我们还会继续讲解.本章我们主要讨论 ...

  6. Asp.net mvc 中View 的呈现(二)

    [toc] 上一节介绍了 Asp.net mvc 中除 ViewResult 外的所有的 ActionResult,这一节介绍 ViewResult. ViewResultBase ViewResul ...

  7. 解析Asp.net Core中使用Session的方法

    2017年就这么悄无声息的开始了,2017年对我来说又是特别重要的一年. 元旦放假在家写了个Asp.net Core验证码登录, 做demo的过程中遇到两个小问题,第一是在Asp.net Core中引 ...

  8. 如何在yii的controller中调用外部action

    问题: 在yii中,一个controller会包含若干个action.有时为了重用或代码管理等目的,我们希望这些action可以单独定义成一个类,然后在controller中使用.那么在yii中要如何 ...

  9. ASP.NET controller TO view 数据传递

    https://stackify.com/viewbag/ In the case of ASP.NET MVC, you have three ways to pass data from the ...

随机推荐

  1. js 动态绑定解绑事件

    function addEvent(obj, type, handle) { if (obj.addEventListener) { obj.addEventListener(type, handle ...

  2. Pandas之loc\iloc\ix

    ---------------------------------------------------------------------------------------------------- ...

  3. Hibernate中Session的save()、update()、merge()、lock()、saveOrUpdate()和persist()方法有什么区别?

    Hibernate的对象有三种状态:瞬态.持久态和游离态.游离状态的实例可以通过调用save().persist()或者saveOrUpdate()方法进行持久化:脱管状态的实例可以通过调用 upda ...

  4. Python3.5-20190504-廖老师的2-if elif else continue break

    条件判断: if 条件1: 代码块 elif 条件2: 代码块 else 条件3: 代码块 brith = input("请输入出身年月:") if  brith > 200 ...

  5. Python和 pytest的异常处理

    Python中有自带的异常处理 try: except: pytest中 1.可以用try except来处理,来保证出错后,把后面的语句执行完成: 2.当有多条用例需要跑完时,不需要考虑其中一条用例 ...

  6. 理解Python中的继承规则和继承顺序

    先来看一段代码: class First(object): def __init__(self): print ("first") class Second(object): de ...

  7. django 邮箱发送

    在django中提供了邮件接口 QQ邮箱配置 qq邮箱地扯:https://mail.qq.com settings文件 # 邮箱配置 EMAIL_USE_SSL = True EMAIL_HOST ...

  8. ubuntu配置px4编译环境

    一.主要参考的内容 px4的开发者手册 https://dev.px4.io/zh/setup/dev_env_linux.html 其中有的shell指令 权限设置 警告:永远不要使用sudo来修复 ...

  9. SCP-Py-002

    项目编号:Py-002 项目等级:EuclidKeter 特殊收容措施: Py-002-1目前被映射在Researcher Kevin的服务器位于Site-Pyproject地下防无线电渗透室且被切断 ...

  10. Service系统服务(五):PXE基础装机环境、配置并验证DHCP服务、配置PXE引导、验证PXE网络装机、PXE+kickstart自动装机

    一.PXE基础装机环境 目标: 本例要求为后续的PXE服务器构建提供RHEL7软件仓库,完成下列任务: 1> 在CentOS真机部署Web目录/var/www/html/rh7dvd   2&g ...