各位大侠大家好~
  小妹初学asp.net,遇到这样几个问题:  我看到一个例子是用windows身份验证,在global.asax中用WindowsAuthentication_OnAuthenticate(Object Source, WindowsAuthenticationEventArgs e)事件处理用户的信息,实例化用户类。
  
现在的问题是: 1、我现在的程序是用forms验证,查了查也有FormsAuthentication_OnAuthenticate这样一个事件,但是不知道这个事件到底怎么用?是怎样触发的? 2、我想在这个事件里实例化我的一个用户类,就是在用户登陆的时候,是不是在这个事件里实例化了之后以后每个叶面里用到这个用户类的时候就不用重新实例化了,直接用就可以?(我看到上面那个用windows验证的那个例子就是这样的) 3、还有:我原来是在用户登陆事件里实例化用户类,然后把这个实例放在session里;如果上面的做法可以的话,把实例放在session里和在FormsAuthentication_OnAuthenticate事件里处理哪个效率更高呢?
  
  附:我看到那个例子程序的处理:
       protected void WindowsAuthentication_OnAuthenticate(Object Source, WindowsAuthenticationEventArgs e) 
        {
            Config conn = new Config();
            string sqlstring = "select ID,NickName,Email,AllowSendRejectMail,AlertEmail,AllowArrInformationalInterview,AllowInformationalInterview,AllowReject,AlertNotify,AllowTest,AllowArrPhoneScreen,AllowPhoneScreen,AllowArrArrangeInterview,AllowArrangeInterview,AllowHire,AllowAdmin from Admin where UserID=@UserID";
            SqlCommand sc = new SqlCommand(sqlstring, conn.myConnection);
            string domain = null,useralias = null;
            MainFun.DevideIntoDomainandUser(e.Identity.Name, ref useralias,ref domain);
            sc.Parameters.Add("@UserID", useralias);
            SqlDataReader sdr = sc.ExecuteReader();            if (sdr.Read())
            {
                LoginUser user = new LoginUser(e.Identity);
                user.UserID = useralias;
                user.BasicView = true;
                user.Nickname = sdr["NickName"].ToString();
                user.ID = sdr["ID"].ToString();
                user.Email = sdr["Email"].ToString();
                user.EmailAlert = (bool)(sdr["AlertEmail"]);
                user.AllowReject = (bool)(sdr["AllowReject"]);
                user.AllowTest = (bool)(sdr["AllowTest"]);
                user.AllowSendRejectMail = (bool)(sdr["AllowSendRejectMail"]);
                user.AllowArrArrangeInterview = (bool)(sdr["AllowArrArrangeInterview"]);
                user.AllowArrPhoneScreen = (bool)(sdr["AllowArrPhoneScreen"]);
                user.AllowArrangeInterview = (bool)(sdr["AllowArrangeInterview"]);
                user.AllowHire = (bool)(sdr["AllowHire"]);
                user.AllowPhoneScreen = (bool)(sdr["AllowPhoneScreen"]);
                user.EmailNotify = (bool)(sdr["AlertNotify"]);
                user.AllowArrInformationalInterview = (bool)(sdr["AllowArrInformationalInterview"]);
                user.AllowInformationalInterview = (bool)(sdr["AllowInformationalInterview"]);
                e.User = user;
                user.AllowAdmin = (bool)(sdr["AllowAdmin"]);
            }
            else
            {
                //e.User = new LoginUser(e.Identity);
                Response.Redirect("Error.htm?noright");
            }
            sdr.Close();
            conn.Close();
        }
    赫赫,问得比较罗嗦,希望各位高手能给与帮助,不胜感激~

解决方案 »

  1.   

    回答:1 你说你使用Form验证,有FormsAuthentication_OnAuthenticate。
    你说错了,FormsAuthentication_OnAuthenticate不是一个事件,它是一个事件处理程序,真正的事件是Authenticate。在global.asax中不过是他的事件处理程序罢了。关于它的定义位置,你可以参考 System.Web.Security.FormsAuthenticationModule 类,这个事件就定义在这个地方。具体是System.Web.Security.FormsAuthenticationModule.Authenticate。
    它是一个HttpModule,如果你不知道什么是HttpModule,请参考.net文档。具体的定义流程是每个Asp.Net站点都是一个HttpApplication,这个HttpApplication也是一个类,定义在System.Web.HttpApplication,在这个类中定义了一个AuthenticateRequest事件,专门触发在用户每次进行Http请求的时候激发验证事件的,而System.Web.Security.FormsAuthenticationModule不过是在这个AuthenticateRequest事件上作了一次包装,重新定义成Authenticate。而FormsAuthenticationModule调用的是System.Web.Security.FormsAuthentication类,它定义了一些静态方法,执行的就是Form验证,具体操作就是读取你的Web.config文件内相关authentication和authorizatio节的内容,并进行判断,而显示用户是否登录。如果你把这几个类的关系搞定了,你就可以自己开发出一个验证模式。.net自带Form验证和Windows验证,你可以自己开发出一个Database验证出来。很容易的。
      

  2.   

    建议参考 www.asp.net 
    Community Starter Kits