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. Docker 在 centos 7上升级

    Docker 在 centos 7上升级 狂暴的蚂蚁 关注 2017.05.22 10:49* 字数 194 阅读 3253评论 0喜欢 1 引子 如果有旧的Docker 先删除 旧Docker版本上 ...

  2. IntelliJ IDEA 常用快捷键和技巧

    IntelliJ Idea 常用快捷键列表 Alt+回车 导入包,自动修正Ctrl+N  查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L  格式化代码Ctrl+Alt+O 优化导入的类和 ...

  3. Feign基础教程

    https://blog.csdn.net/u010862794/article/details/73649616 简介 Feign使得 Java HTTP 客户端编写更方便.Feign 灵感来源于R ...

  4. go语言从例子开始之Example3.变量

    在 Go 中,变量 被显式声明,并被编译器所用来检查函数调用时的类型正确性 package main import "fmt" func main() { var 声明 1 个或者 ...

  5. Linux下的上传和下载yum install -y lrzsz

    先使用命令 yum install -y lrzsz rz 上传    或者直接拖动 sz 要下的文件 回车

  6. 2.第一个Codefirst实例

    1.什么是codefirst EF4.1中开始支持Code First .这种方式在领域设计模式中非常有用.使用Code First模式,你可以专注于领域设计,根据需要,为你一个领域的对象创建类集合, ...

  7. BeanUtils.copyProperties()拷贝属性时,忽略空值

    把source的属性值复制给target的相同属性上,注意:双方需要复制的属性要有get.set方法 BeanUtils.copyProperties(source, target, PublicUt ...

  8. cocos2D-X call JNIHelper

    #ifndef _WIN32 JNIEnv *j = JniHelper::getEnv(); if (j == nullptr || j == NULL) {test += "JNIEnv ...

  9. vue中引入路由,如果你懒得写那么

    可以npm i vue-router --save,项目中自动给你写好,vuex也可以

  10. POJ 3904 (莫比乌斯反演)

    Stancu likes space travels but he is a poor software developer and will never be able to buy his own ...