特色栏目

ASP源码

PHP源码

.NET源码

JSP源码

游戏频道
专题合集
关闭菜单
首页> ASP.NET教程> ASP.NET—From验证:全部代码及讲解

ASP.NET—From验证:全部代码及讲解

时间:2009-07-01 16:12:53 作者:互联网

关于Forms验证的文章网上千百篇,但我花了1天半的时间学会了“一点点”,
现在把代码分享出来,希望对像我一样的初学者所有帮助,也希望高手给指点一下:

--------------------------------------------------------------------------------

Step 1:新建数据库(库:MyForms ;表:users ;字段:ID,userName, userPwd);
Step 2:新建网站,we***onfig 的文件全部代码如下:


we***onfig 的全部代码


   
   
 
    <sy***m.web>
       
   
   
   
   
       
      lo***.aspx" protection="All">
     
   

   
   
     
   

   
    Ge***icErrorPage.htm">
        No***ess.htm" />
        Fi***otFound.htm" />
   

   
    sy***m.web>
 

Step 3:添加一个 lo***.aspx  页面;拖2个 TextBox ,1个Button 和1个CheckBox ;
           并将CheckBox 的text 属性设为:“是否保存Cookis ";
Step 4:lo***.aspx 的隐藏代码如下:

login 全部隐藏代码
using System;
using Sy***m.Data;
using Sy***m.Configuration;
using Sy***m.Web;
using Sy***m.Web.Security;
using Sy***m.Web.UI;
using Sy***m.Web.UI.WebControls;
using Sy***m.Web.UI.WebControls.WebParts;
using Sy***m.Web.UI.HtmlControls;
using Sy***m.Data.SqlClient; //导入命名空间

public partial class _Default : Sy***m.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string userName = Te***ox1.Text.Trim();
        string userPwd = Te***ox2.Text.Trim();
        SqlConnection con = new SqlConnection("Server=.;Database=MyForms;User ID=sa;Password=123456");
        con.Open();
        SqlCommand cmd = new SqlCommand("select count(*) from users where userName='" + userName + "' and userPwd='" + userPwd + "'", con);
        int count = Co***rt.ToInt32(cm***xecuteScalar());
        if (count > 0)
        {
            Sy***m.Web.Security.FormsAuthentication.SetAuthCookie(th***TextBox1.Text, th***CheckBox1.Checked);
            Re***nse.Redirect("De***lt.aspx");
            //上面两行,也可以换成下面一行,如通过验证则直接转向请求的页面,而不需要Re***nsel.Redirect("");
            //Sy***m.Web.Security.FormsAuthentication.RedirectFromLoginPage(th***TextBox1.Text, false);  
        }

        else
        {
            Re***nse.Write("用户不合法");
        }      
    }
}

Step 5:拖一个Button 到 De***lt.aspx 上,将其text 属性设为"登出",其事件代码如下:

Button 事件代码
protected void Button1_Click(object sender, EventArgs e)
    {
        Sy***m.Web.Security.FormsAuthentication.SignOut();
    }

http://www.cnblogs.com/yoyebina/archive/2006/12/03/580121.html

相关文章 最新文章

相关应用

热门文章

猜你喜欢

返回顶部