Form 表单认证,简单还是复杂?
来源:广州中睿信息技术有限公司
发布时间:2012/10/21 23:25:16 编辑:itlead 阅读 1970

  一个简单的Form 表单认证 

  设置Web.config

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

  <authentication mode="Forms">

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

  </authentication>

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

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

  //登录代码

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

  {

  //方法 a) 指验证后返回请求页面,俗称“从哪来就打哪去”。

  if(this.txtUserName.Text=="Admin" && this.txtPassword.Text=="abcdef")

  {

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

  }

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

  // if(this.txtUserName.Text=="Admin" && this.txtPassword.Text=="abcdef")

  // {

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

  // Response.Redirect("Default.aspx");

  // }

  }

  //判断用户是否已经登录

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

  {

  // 在此处放置用户代码以初始化页面

  //Response.Write("用户名:"+User.Identity.Name+"<br>"+"验证:"+User.Identity.IsAuthenticated);

  if(User.Identity.IsAuthenticated)

  {

  //认证后内容自己添加

  }

  }

  //退出Form认证

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

  {

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

  }

  

  

本站技术原创栏目文章均为中睿原创或编译,转载请注明:文章来自中睿,本站保留追究责任的权利。