要求使用Application和Session对象统计登录人数
要求在Web.config中配置允许Admin和PowerUser角色,拒绝Customers角色和其他匿名用户访问我如何写呢??高手写下~

解决方案 »

  1.   

        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = this.txtName.Text.Trim();
            string pwd = this.txtPwd.Text.Trim();
            //if (name == "admin" && pwd == "123" || name == "fubing" && pwd == "123")
            //{
            //    //验证通过,立即产生一个身份认证票据(该票据的主要内容就是一个身份认证Cookie)
            //    //然后跳转到刚请求的页面
            //    FormsAuthentication.RedirectFromLoginPage(name,false);
            //}        //验证配置文件中的用户名和密码
            if (FormsAuthentication.Authenticate(name, pwd) == true)
            {
                FormsAuthentication.RedirectFromLoginPage(name, false);
            }
        }
      

  2.   

     protected void Page_Load(object sender, EventArgs e)
        {
    /*HttpContext.Current.User代表当前用户的信息(包括匿名用户)
             * HttpContext.Current.User.Identity.AuthenticationType返回该用户的登陆方式
             * HttpContext.Current.User.Identity.IsAuthenticated返回当前用户是否经过验证
             * HttpContext.Current.User.Identity.Name返回经过认证的用户的用户名
             */
            this.lblUserName.Text = HttpContext.Current.User.Identity.Name;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string mima = this.TextBox1.Text;
    //FormsAuthentication.HashPasswordForStoringInConfigFile的作用是用指定的加密算法加密字符串,返回加密后得到的字符串
            string aftermima = FormsAuthentication.HashPasswordForStoringInConfigFile(mima, "sha1");
            Response.Write(aftermima);
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            //FormsAuthentication.SignOut()方法会删除身份认证Cookie(它的作用相当于Session.Abandon()),一般用来注销当前登录用户
            FormsAuthentication.SignOut();
            //FormsAuthentication.RedirectToLoginPage()的作用是跳转到登陆页面去
            FormsAuthentication.RedirectToLoginPage();
        }