基于Cookie简易的权限判断代码,需要的朋友可以参考下。

写入Cookie页面,创建cookie后,设置cookie属性,并添加到Response.Cookies中读取cookie,利用cookie的名字或索引从Request.Cookies中取得改写Cookie,先创建一个同名的cookie,读取Request中同名的cookie,把读取cookie的属性值付给新的对象,加入到Response.Cookies中创建一个BasePage页面,其他的页面继承自这个页面,把权限判断的代码有单个页面的Page_Load转移到BasePage的PreLoad中,下面是BasePage的主要代码

public class BasePage : System.Web.UI.Page 

private string pageName; 
public BasePage() 

this.Page.PreLoad += Page_Load; 

protected void Page_Load(object sender, EventArgs e) 

if (!IsPostBack) 

Uri r = this.Request.Url; 
pageName = r.AbsolutePath; 
if (NeedToCheck()) 

if (!HasAuthentication()) 

HttpContext.Current.Response.Redirect("NoAuthenticationPage.aspx"); 




private bool NeedToCheck() 

if (pageName.Contains("NoAuthenticationPage.aspx") || pageName == "Login.aspx" ) 

return false; 

return true; 

private bool HasAuthentication() 

//look into the config file or database,to see whether this page is in the allow accessing list of the role or not; 
//the signature of the function is like this 
//QueryInConfig(m_UserRole,pageName); 
if (pageName.Contains("Default3.aspx") && UserRole == "2") 

return false; 

return true; 

protected HttpCookie _RequestCookie; 
protected HttpCookie _ResponseCookie; 
private bool b_IsNewCookie = true; 
public string UserRole 

get 

return GetCookieValue("UserRole"); 

set 

SetCookieValue("UserRole", value); 


public string UserName 

get 

return GetCookieValue("UserName"); 

set 

SetCookieValue("UserName", value); 


protected void SetCookieValue(string name, string value) 

SetResponseCookie(); 
_ResponseCookie[name] = value; 

private string GetCookieValue(string name) 

SetReqeustCookie(); 
if (_RequestCookie != null) 

return _RequestCookie[name]; 

return null; 

protected void SetReqeustCookie() 

_RequestCookie = HttpContext.Current.Request.Cookies["Cookie_Name"]; 

protected void SetResponseCookie() 

if (b_IsNewCookie) 

HttpContext.Current.Response.Cookies.Remove("Cookie_Name"); 
_ResponseCookie = new HttpCookie("Cookie_Name"); 
DateTime dtNow = DateTime.Now; 
TimeSpan tsMinute = new TimeSpan(0, 2, 0, 0); 
_ResponseCookie.Expires = dtNow + tsMinute; 
_ResponseCookie["UserRole"] = UserRole; 
_ResponseCookie["UserName"] = UserName; 
HttpContext.Current.Response.Cookies.Add(_ResponseCookie); 
b_IsNewCookie = false; 


}

Asp.net 基于Cookie简易的权限判断的更多相关文章

  1. ASP.NET -- WebForm -- Cookie的使用 应用程序权限设计 权限设计文章汇总 asp.net后台管理系统-登陆模块-是否自动登陆 C# 读写文件摘要

    ASP.NET -- WebForm -- Cookie的使用 ASP.NET -- WebForm --  Cookie的使用 Cookie是存在浏览器内存或磁盘上. 1. Test3.aspx文件 ...

  2. 尝试asp.net mvc 基于controller action 方式权限控制方案可行性

    微软在推出mvc框架不久,短短几年里,版本更新之快,真是大快人心,微软在这种优秀的框架上做了大量的精力投入,是值得赞同的,毕竟程序员驾驭在这种框架上,能够强力的精化代码,代码层次也更加优雅,扩展较为方 ...

  3. Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员、后台管理员同时登录

    1.登录的实现 登录功能实现起来有哪些常用的方式,大家首先想到的肯定是cookie或session或cookie+session,当然还有其他模式,今天主要探讨一下在Asp.net core 2.0下 ...

  4. 尝试asp.net mvc 基于controller action 方式权限控制方案可行性(转载)

    微软在推出mvc框架不久,短短几年里,版本更新之快,真是大快人心,微软在这种优秀的框架上做了大量的精力投入,是值得赞同的,毕竟程序员驾驭在这种框架上,能够强力的精化代码,代码层次也更加优雅,扩展较为方 ...

  5. ASP.NET Core Authentication系列(四)基于Cookie实现多应用间单点登录(SSO)

    前言 本系列前三篇文章分别从ASP.NET Core认证的三个重要概念,到如何实现最简单的登录.注销和认证,再到如何配置Cookie 选项,来介绍如何使用ASP.NET Core认证.感兴趣的可以了解 ...

  6. 理解ASP.NET Core - 基于Cookie的身份认证(Authentication)

    注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或点击此处查看全文目录 概述 通常,身份认证(Authentication)和授权(Authorization)都会放 ...

  7. Asp.Net Core基于Cookie实现同域单点登录(SSO)

    在同一个域名下有很多子系统 如:a.giant.com  b.giant.com   c.giant.com等 但是这些系统都是giant.com这个子域. 这样的情况就可以在不引用其它框架的情况下, ...

  8. Asp.Net Core 项目实战之权限管理系统(5) 用户登录

    0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...

  9. Asp.Net Core 项目实战之权限管理系统(6) 功能管理

    0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...

随机推荐

  1. 小二助手-react.js分块加载

    小二助手在线演示地址:http://118.25.217.253:8000  账号test 密码123 小二助手是用material-ui开发的,感觉国内使用的人数不是特别多,所以创建了一个qq交流群 ...

  2. Hive中日期函数总结

    --Hive中日期函数总结: --1.时间戳函数 --日期转时间戳:从1970-01-01 00:00:00 UTC到指定时间的秒数 select unix_timestamp(); --获得当前时区 ...

  3. Kubernetes用户指南(四)--应用检查和调试

    一.调试 当你的应用开始运行,那么DEBUG是不可避免的问题. 早些时候,我们在描述的是如何通过kubectl get pods来获得Pod的简单状态信息. 但是现在,这里有更多的方式来获得关于你的应 ...

  4. hadoop提交作业自定义排序和分组

    现有数据如下: 3 3 3 2 3 1 2 2 2 1 1 1 要求为: 先按第一列从小到大排序,如果第一列相同,按第二列从小到大排序 如果是hadoop默认的排序方式,只能比较key,也就是第一列, ...

  5. REOBJECT structure

    REOBJECT structure   包含丰富编辑控件中的OLE或图像对象的信息. Syntax 语法 typedef struct _reobject { DWORD cbStruct; LON ...

  6. [Functional Programming] Combine Multiple State ADT Instances with the Same Input (converge(liftA2(constant)))

    When combining multiple State ADT instances that depend on the same input, using chain can become qu ...

  7. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何动态显示当前运行行

    在F11运行状态下,点击Online-Display Flow Control然后可以看到绿色的行就是当前正在运行行     更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http:// ...

  8. DEB方式在UBUNTU安装ODOO 8.0

    odoo在ubuntu最简单最快速安装方式是deb方式,基本无需再去改数据库配置文件,全自动化了,odoo中文网推荐新手采用此方法 1 安装数据库:sudo apt-get install postg ...

  9. acm之路--母函数 by小宇

    母函数又叫生成函数,原是数学上的一个名词,是组合数学中的一个重要理论. 生成函数是说,构造这么一个多项式函数g(x).使得x的n次方系数为f(n). 对于母函数,看到最多的是这样两句话: 1.&quo ...

  10. lucene 查询

    csdn blog - Lucene 3.0 的Query Parser(查询语法)   ibm developerWorks - 使用 Apache Lucene 2.4.1 搜索文本   osch ...