spiderman

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

本文主要针对目前应用广泛的一些权限验证方式进行简单的对比总结,学识有限,欢迎大家批评指正。^-^

 

1、 Forms验证

没什么好说的,配置文件,好不好用是仁者见仁智者见智的事了,呵呵

参考: 基于aspnet Forms身份验证基本原理

地址:http://www.cnblogs.com/KUDO/archive/2006/08/28/488343.html

2、 母版页中嵌入验证代码 Master.Master.cs

protected void Page_Load(object sender, EventArgs e)

        {

            English.UI.WebGlobal.IsAdminLogin();

        }

    缺点:不够灵活,且master的load事件在内容页load之后加载,效果不理想

3、 用户控件验证

    缺点:有局限,只有包含该用户控件的页面才能进行验证。

4、 继承:灵活,方便,牵一发而动全身的推荐选择

 

详细分析请参考:利用.net的内部机制在asp.net中实现身份验证

地址:http://zhoufoxcn.blog.51cto.com/792419/166380

代码参考李天平的pagebase.cs代码:

using ...

namespace LTP.Common

{

    //<summary>

    // 页面层(表示层)基类,所有页面继承该页面

    //</summary>

    public class PageBase:System.Web.UI.Page

    {

        public int PermissionID = -1;默认-1为无限制,可以在不同页面继承里来控制不同页面的权限

        string virtualPath = LTP.Common.ConfigHelper.GetConfigString("VirtualPath");

                     

        //<summary>

        //构造函数

        //</summary>

       public PageBase()

        {

//在基类的构造函数中定义基类的加载事件的处理方法,在这个方法里检查用户是否登录。

//至于为什么要选择这个事件而不用其它事件,那是因为有些事件发生得还是太早,怕SessionCookie还是不可用,

//有些事件又太晚,用加载事件是刚刚好【http://zhoufoxcn.blog.51cto.com/792419/166380

           this.Load+=new EventHandler(PageBase_Load);

        }

        //protected override void OnInit(EventArgs e)

        //{

        //    base.OnInit(e);

        //    this.Load += new System.EventHandler(PageBase_Load);

        //    this.Error += new System.EventHandler(PageBase_Error);

        //}

        //错误处理

        protected void PageBase_Error(object sender, System.EventArgs e)

        {

           

        }

       

//        private void PageBase_Load(object sender, EventArgs e)
//        {
//            if (!Page.IsPostBack )
//            {

//                //权限验证
//                if (Context.User.Identity.IsAuthenticated)
//                {
//                    AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
//                    if (Session["UserInfo"] == null)
//                    {
//                        LTP.Accounts.Bus.User currentUser = new LTP.Accounts.Bus.User(user);
//                        Session["UserInfo"] = currentUser;
//                        Session["Style"] = currentUser.Style;
//                        Response.Write("<script defer>location.reload();</script>");
//                    }
//                    if ((PermissionID != -1) && (!user.HasPermissionID(PermissionID)))
//                    {
//                        Response.Clear();
//                        Response.Write("<script defer>window.alert('您没有权限进入本页!请重新登录或与管理员联系');history.back();</script>");
//                        Response.End();
//                    }
//                }
//                else
//                {
//                    FormsAuthentication.SignOut();
//                    Session.Clear();
//                    Session.Abandon();
//                    Response.Clear();
//                    Response.Write("<script defer>window.alert('您没有权限进入本页或当前登录用户已过期!请重新登录或与管理员联系!');parent.location='" + virtualPath + "/Login.aspx';</script>");
//                    Response.End();
//                }  
//            }           
   
//        }
//    }

    }

posted on 2009-06-24 16:09  spiderman  阅读(480)  评论(0)    收藏  举报