ASP.NET Form身份认证

通过修改web.config,使用Form认证来控制访客的权限

1、

<authentication mode="Windows" /> 把它改成:

<authentication mode="Forms">

<forms loginUrl="Login.aspx" name=".ASPXAUTH"></forms>

</authentication>

2、 找到<authorization> <allow users="*" /></authorization>换成

<authorization><deny users="?"></deny></authorization>

前台登陆界面:

      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、

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 使用框架结构的系统。

3、 退出代码:

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

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

}

4、注意一点:此时不能给项目指定开始页面,除非是你在web.config 设置的 loginUrl 页面

posted on 2013-10-20 13:40  Jeacathy  阅读(201)  评论(0编辑  收藏  举报