这里分享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 权限验证的更多相关文章

  1. NET MVC权限验证

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

  2. .net web api 权限验证

    做一个登录权限验证. 开始吧. using System; using System.Collections.Generic; using System.Drawing; using System.D ...

  3. 关于filter web api mvc 权限验证 这里说的够详细了。。。

    参考:http://www.cnblogs.com/willick/p/3331520.html Filter(筛选器)是基于AOP(面向方面编程)的设计,它的作用是对MVC框架处理客户端请求注入额外 ...

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

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

  5. mvc 权限验证

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  6. MVC权限验证过滤器

    Action属性,权限设定属性   [AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)] ...

  7. MVC权限验证之ActionFilterAttribute

    参考:http://www.cnblogs.com/waitingfor/archive/2011/12/27/2303784.html ActionFilterAttribute是Action过滤类 ...

  8. C# MVC权限验证

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

  9. asp.net web api 权限验证的方法

    思路:客户端使用header或者form讲验证信息传入api,在权限验证过滤中进行处理,代码示例: 定义过滤器 public class ApiFilter1 : System.Web.Http.Au ...

随机推荐

  1. LiunxCentos7 上安装 FastDFS

    Centos7 上安装 FastDFS 1.安装gcc(编译时需要) FastDFS是C语言开发,安装FastDFS需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gc ...

  2. woocommerce如何隐藏/显示product meta

    前面我们说了woocommerce如何隐藏SKU,那如果不想显示产品分类category和标签tag呢?我们知道SKU, Category list 和 Tag list在woocommerce产品页 ...

  3. 5 Ways AI is Transforming the Finance Industry

    https://marutitech.com/ways-ai-transforming-finance/ As global technology has evolved over the years ...

  4. P256 VRF实现解读

    目录 P256 VRF实现及其改造 公式推导 H1:把任意信息映射到曲线上的点 H2: 映射任意信息为(1,q) 计算随机数 随机数的proof 如何验证 VRF优点 针对S256曲线的改造 1. 使 ...

  5. flink源码学习

    传送门: https://www.jianshu.com/c/c9b356caf8a6

  6. 基于paramiko将文件上传到服务器上

    通过安装使用paramiko模块,将本地文件上传到服务器上 import paramiko import datetime import os hostname = '服务器ip' username ...

  7. [LeetCode] 890. Find and Replace Pattern 查找和替换模式

    You have a list of words and a pattern, and you want to know which words in words matches the patter ...

  8. [LeetCode] 633. Sum of Square Numbers 平方数之和

    Given a non-negative integer c, your task is to decide whether there're two integers a and b such th ...

  9. ansys meshing划分无厚度的面

    优酷播放地址: https://v.youku.com/v_show/id_XNDQ3MjAyODYzMg==.html?spm=a2hzp.8244740.0.0 原版视频教程下载地址: https ...

  10. GreenPlum 大数据平台--segment 失效问题恢复《二》(全部segment宕机情况下)

    01,情况描述 主Segment和它的镜像都宕掉.导致了greenplum数据库不可用状态 02,重启greenplum数据库 gpstop -r 03,恢复 gprecoverseg 04,状态检查 ...