当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 使用HttpContext中的User属性来实现用户身份验证之用户验证票篇

ASP.NET
asp.net 使用Silverlight操作ASPNETDB数据库
ASP.NET 前后台调用方法
ASP.NET中等安全模式的一些经验分享
asp.net 打印控件使用方法
网站开发技术:ASP.NET 2.0搭建网站
ASP.NET实例教程:创建数据透视表
ASP.NET 4进行SEO优化提高网站排名和权重
ASP.NET实例教程:订阅 GeoRSS 订阅源
ASP.NET页面间数据传递的9种方法
ASP.NET教程:网页表单多个按钮完成不同功能
Asp.net的服务器推技术 (Server Push)
asp.net 无刷新附件上传实现方法
ASP.NET 定制简单的错误处理页面实现代码
c# 在WebBrowser中用SendMessage模拟鼠标点击
asp.net 表单验证新思路
.NET从优酷专辑中采集所有视频及信息(VB.NET代码)
VS2005 水晶报表在时部署时遇到的问题
ASP.NET 输出图片简单代码
一天精通asp.net的学习经验小结
DataGridView中绑定DataTable数据及相关操作实现代码

ASP.NET 中的 使用HttpContext中的User属性来实现用户身份验证之用户验证票篇


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 71 ::
收藏到网摘: n/a

在我的上一篇《使用HttpContext中的User属性来实现用户身份验证》中已经讲了怎样来使用HttpContext.User属性来实现用户身份验证,并且还写了一个示例程序。但是,在上一篇文章中,我们使用的是系统缓存来保存用户的登录信息,这无疑是占用系统资源的一种做法,那有没有更好的办法呢?我在上一章中说过大家可以尝试使用用户验证票的方式来保存用户登录信息的,这种方式是基于Cookie原理来实现的,因此避免了使用缓存所带来的困扰。我们已经有了上一篇的基础,这次只需要在它的基础上稍加修改就可以了。 要使用这种方法,我们首先要配置web.config文件,把节点的属性mode改为Forms,再在该节点中添加,这些代表的意思可以在MSDN上查到,这里就不多作解释了。下面是修改后的代码: MyPage.cs using System;using System.Collections; namespace HttpContextUserEG{ /// /// MyPage 的摘要说明。 /// /// 继承自Page类 public class MyPage : System.Web.UI.Page { public MyPage() { // // TODO: 在此处添加构造函数逻辑 // } protected override void OnInit(EventArgs e) { base.OnInit (e); this.Load +=new EventHandler(MyPage_Load); } //在页面加载的时候从缓存中提取用户信息 private void MyPage_Load(object sender, System.EventArgs e) { if(Context.User.Identity.IsAuthenticated) { if(!(Context.User is MyPrincipal)) { MyPrincipal principal = new MyPrincipal(Context.User.Identity.Name); Context.User = principal; } } } }} MyPrincipal.cs using System;using System.Collections; namespace HttpContextUserEG{ /// /// MyPrincipal 的摘要说明。 /// /// 实现IPrincipal接口 public class MyPrincipal : System.Security.Principal.IPrincipal { private System.Security.Principal.IIdentity identity; private ArrayList roleList; public MyPrincipal(string userID) { // // TODO: 在此处添加构造函数逻辑 // identity = new MyIdentity(userID); roleList = new ArrayList(); roleList.Add("Admin"); } public static MyPrincipal ValidateLogin(string userID,string password) { if(userID == "yan0lovesha" && password == "iloveshasha") { return new MyPrincipal(userID); } else return null; } public ArrayList RoleList { get { return roleList; } } #region IPrincipal 成员 public System.Security.Principal.IIdentity Identity { get { // TODO: 添加 MyPrincipal.Identity getter 实现 return identity; } set { identity = value; } } public bool IsInRole(string role) { // TODO: 添加 MyPrincipal.IsInRole 实现 return roleList.Contains(role);; } #endregion }} MyIdentity.cs using System; namespace HttpContextUserEG{ /// /// MyIdentity 的摘要说明。 /// /// 实现IIdentity接口 public class MyIdentity : System.Security.Principal.IIdentity { private string userID; private string password; public MyIdentity(string currentUserID) { // // TODO: 在此处添加构造函数逻辑 // userID = currentUserID; password = "iloveshasha"; //这里实际上是从数据库中获得的密码 } private bool CanPass() { //这里朋友们可以根据自己的需要改为从数据库中验证用户名和密码, //这里为了方便我直接指定的字符串 if(userID == "yan0lovesha" && password == "iloveshasha") { return true; } else { return false; } } public string Password { get { return password; } set { password = value; } } #region IIdentity 成员 public bool IsAuthenticated { get { // TODO: 添加 MyIdentity.IsAuthenticated getter 实现 return true; } } public string Name { get { // TODO: 添加 MyIdentity.Name getter 实现 return userID; } } //这个属性我们可以根据自己的需要来灵活使用,在本例中没有用到它 public string AuthenticationType { get { // TODO: 添加 MyIdentity.AuthenticationType getter 实现 return null; } } #endregion }} WebForm.aspx.cs using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Drawing;using System.Web;using System.Web.Caching;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls; namespace HttpContextUserEG{ /// /// WebForm1 的摘要说明。 /// /// 将这里本来继承自Page类改为继承自我们自己的MyPage类 public class WebForm1 : HttpContextUserEG.MyPage { protected System.Web.UI.WebControls.T