1. 根目录下的web.config

 

>

 

 

2.public目录下的web.config,用于放匿名用户也能浏览的页面
>
3.admin目录下的web.config,用于放只有用户名为admin的用户才能浏览
>
4. 在login.aspx.cs简单一句验证就可以了
);
5. 还有一些参考代码

1  登录代码:

a  书本上介绍的

         private void Btn_Login_Click(object sender, System.EventArgs e)

         {

             if(this.Txt_UserName.Text=="Admin" && this.Txt_Password.Text=="123456")

              {

                   System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.Txt_UserName.Text,false);

      }

}

b  偶找了 N 久才找到的

private void Btn_Login_Click(object sender, System.EventArgs e)

         {

             if(this.Txt_UserName.Text=="Admin" && this.Txt_Password.Text=="123456")

              {

System.Web.Security.FormsAuthentication.SetAuthCookie(this.Txt_UserName.Text,false);

     Response.Redirect("Default.aspx");

     }

}

以上两种都可发放验证后的Cookie,即通过验证,区别:

方法 a) 指验证后返回请求页面,俗称“从哪来就打哪去”。比如:用户没登录前直接在 IE 地址栏输入http://localhost/FormTest/UserInfo.aspx,那么该用户将看到的是Login.aspx?ReturnUrl=UserInfo.aspx,输入用户名与密码登录成功后,系统将根据“ReturnUrl”的值,返回相应的页面

方法 b) 则是分两步走:通过验证后就直接发放Cookie,跳转页面将由程序员自行指定,此方法多用于Default.aspx 使用框架结构的系统。 

2  退出代码:

private void Btn_LogOut_Click(object sender, System.EventArgs e)

     {

System.Web.Security.FormsAuthentication.SignOut();

}

3. 检查是否通过验证

 

if(User.Identity.IsAuthenticated)

         {

              //你已通过验证,知道该怎么做了吧?

}

User.Identity还有两个属性AuthenticationType(验证类型)与Name(用户名称),大家要注意的是Name属性,此处的User.Identity.Name将得到,验证通过(RedirectFromLoginPageSetAuthCookie)时,我们带入的第一个参数this.Txt_UserName.Text 

 

];
          }
          Response.Redirect(returnUrl1);
    }
}

 

 

 

相关文章:

  • 2021-06-12
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-02
  • 2022-01-19
猜你喜欢
  • 2022-12-23
  • 2022-03-07
  • 2021-10-21
  • 2021-10-05
  • 2021-06-05
  • 2021-12-07
相关资源
相似解决方案