.net web mvc 权限验证
这里分享MVC的权限验证,内容中可能存在一些,莫名其妙的方法,那些是以前封装好的,大致可以根据方法名称知道他的意思。
using Game.Entity;
using Game.Entity.PlatformManager;
using Game.Facade;
using Game.Utils.Cache;
using Game.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security; namespace Game.Web.Authorize
{
/// <summary>
/// web mvc 管理员权限验证
/// </summary>
public class AdminLoginAuthorize : AuthorizeAttribute
{
/// <summary>
/// 管理员权限验证
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
bool result = false;
if (httpContext.User != null)
{
//获取用户票证
var ticObject = WHCache.Default.Get<CookiesCache>(FormsAuthentication.FormsCookieName);
if (ticObject != null)
{
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(ticObject.ToString());
if (!ticket.Expired && ticket.CookiePath == httpContext.Request.UserHostAddress)
{
//获取Session缓存用户
var user = FacadeManage.aidePlatformManagerFacade.GetUserInfoFromCache();
if (user == null)
{
user = FacadeManage.aidePlatformManagerFacade.GetUserByUserID(int.Parse(ticket.Name.Split('_')[]));
FacadeManage.aidePlatformManagerFacade.SaveUserCache(user);
}
//验证Session缓存用户是否和登录用户一致
if (ticket.Name == $"UserToken_{user.UserID}")
{
result = true;
}
}
}
}
return result;
} /// <summary>
/// 处理授权失败的 HTTP 请求。
/// </summary>
/// <param name="filterContext">封装用于 System.Web.Mvc.AuthorizeAttribute 的信息。 filterContext 对象包括控制器、HTTP 上下文、请求上下文、操作结果和路由数据。</param>
protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
{
string _errorMsg = "出错了";
if (filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true)
|| filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true))
{
return;
} //异步请求
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
filterContext.Result = new JsonResult()
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = new
{
ErrorType = this.GetType().Name,
Action = filterContext.ActionDescriptor.ActionName,
Message = _errorMsg
}
};
}
else
{
filterContext.Result = new RedirectResult("/Home/Login");
}
}
}
}
.net web mvc 权限验证的更多相关文章
- NET MVC权限验证
ASP.NET MVC权限验证 封装类 写该权限类主要目地 为了让权限配置更加的灵活,可以根据SQL.json.或者XML的方式来动态进行页面的访问控制,以及没有权限的相关跳转. 使用步骤 1.要建一 ...
- .net web api 权限验证
做一个登录权限验证. 开始吧. using System; using System.Collections.Generic; using System.Drawing; using System.D ...
- 关于filter web api mvc 权限验证 这里说的够详细了。。。
参考:http://www.cnblogs.com/willick/p/3331520.html Filter(筛选器)是基于AOP(面向方面编程)的设计,它的作用是对MVC框架处理客户端请求注入额外 ...
- ASP.NET MVC权限验证 封装类
写该权限类主要目地 为了让权限配置更加的灵活,可以根据SQL.json.或者XML的方式来动态进行页面的访问控制,以及没有权限的相关跳转. 使用步骤 1.要建一个全局过滤器 //受权过滤器 publi ...
- mvc 权限验证
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- MVC权限验证过滤器
Action属性,权限设定属性 [AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)] ...
- MVC权限验证之ActionFilterAttribute
参考:http://www.cnblogs.com/waitingfor/archive/2011/12/27/2303784.html ActionFilterAttribute是Action过滤类 ...
- C# MVC权限验证
前言 之前一直没怎么接触过权限验证这块,刚好公司老平台改版,就有了这篇权限验证.此篇文章大致讲解下 精确到按钮级别的验证如何实现.以及权限验证设计的参考思路(菜鸟一枚,大神勿喷). 在开发大项目的时候 ...
- asp.net web api 权限验证的方法
思路:客户端使用header或者form讲验证信息传入api,在权限验证过滤中进行处理,代码示例: 定义过滤器 public class ApiFilter1 : System.Web.Http.Au ...
随机推荐
- vs解决方案文件出错
问题描述: 电脑死机,重启电脑后打开解决方案,提示“选择的文件不是有效的解决方案文件” 解决方案: 1. 先用记事本打开这个解决方案查看下,发现其中内容变成空白了? 2. 打开项目中的xxxx.vcx ...
- day4_常用的内置函数
# dict() 工厂函数, 生成字典 a = dict(u=2, i="hello", **{"kk":"cc"}) a = dict(y ...
- LeetCode24-Swap_Pairs
swapPairs public ListNode swapPairs(ListNode head) { if(head==null ||head.next==null) return head; L ...
- ipc.Client: Retrying connect to server: .../10.0.0.27:10020. Already tried 6 time(s); retry policy is RetryUpToMaximumCountWithFixedSleep(maxRetries=10, sleepTime=1 SECONDS)
运行 时候爆出这个错 Exception in thread "main" java.io.IOException: java.net.ConnectException: Call ...
- 创建vector<T>容器
vector<T> 容器是包含 T 类型元素的序列容器,和 array<T,N> 容器相似,不同的是 vector<T> 容器的大小可以自动增长,从而可以包含任意数 ...
- 学习:逆向PUSH越界/INT 68/反调试导致的程序
自己根据shark恒老师的分析,总结一下: 一般反调试自动关闭程序利用的函数有: 1.CreateToolhelp32Snapshot 2.FindWindow 3.ExitProcess 4.Pos ...
- ProceedingJoinPoint获取实现类接口上的注解
使用aspectj处理拦截aop,需要获取实现类接口上的注解 public Object around(ProceedingJoinPoint pjp) throws Throwable{ long ...
- Shell编程——test命令
1.整数 如果表达式为真返回值为0,如果表达式为假,返回值为1.test命令可以对整数.字符串.以及文件进行判断. -it:小于 -le:小于或等于 -gt:大于 -ge:大于或等于 -eq:等于 - ...
- vue项目的安装与启动
第一步:安装Node 检测Node是否安装成功 node -v 第二步:安装vue-cli 命令:npm install vue-cli -g 第三步:搭建项目命令 vue init we ...
- 在树莓派上配置MariaDB
在树莓派上配置MariaDB 前言 MariaDB是由原本开发MySQL的一些原始开发者领导,他们担心Oracle收购MySQL后会有一些隐患.MariaDB与MySQL保持这高度兼容性,并使用了一个 ...