国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.NET—From驗證:全部代碼及講解

2019-11-18 16:50:31
字體:
來源:轉載
供稿:網友

關于Forms驗證的文章網上千百篇,但我花了1天半的時間學會了“一點點”,
現在把代碼分享出來,希望對像我一樣的初學者所有幫助,也希望高手給指點一下:

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

Step 1:新建數據庫(庫:MyForms ;表:users ;字段:ID,userName, userPwd);
Step 2:新建網站,web.config 的文件全部代碼如下:


web.config 的全部代碼
<?xml version="1.0"?>
<configuration>
    <appSettings/>
    <connectionStrings/>
 
    <system.web>
        <compilation debug="true"/>
   
    <sessionState cookieless="AutoDetect"/>
    <!--解決當瀏覽器端禁用Cookie時-->
   
        <authentication mode="Forms">
      <forms name="CookieName" loginUrl="login.aspx"       <!--loginUrl為登錄面URL,如果沒有身份驗證Cookie,客戶端將被重定向到此URL-->
    </authentication>
   
    <authorization>
      <deny users="?"/>
    </authorization>
   
    <customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
        <error statusCode="403" redirect="No
access.htm" />
        <error statusCode="404" redirect="FileNotFound.htm" />
    </customErrors>
   
    </system.web>
 
</configuration>

Step 3:添加一個 login.aspx  頁面;拖2個 TextBox ,1個Button 和1個CheckBox ;
           并將CheckBox 的text 屬性設為:“是否保存Cookis ";
Step 4:login.aspx 的隱藏代碼如下:

login 全部隱藏代碼
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; //導入命名空間

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string userName = TextBox1.Text.Trim();
        string userPwd = TextBox2.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 = Convert.ToInt32(cmd.ExecuteScalar());
        if (count > 0)
        {
            System.Web.Security.FormsAuthentication.SetAuthCookie(this.TextBox1.Text, this.CheckBox1.Checked);
            Response.Redirect("Default.aspx");
            //上面兩行,也可以換成下面一行,如通過驗證則直接轉向請求的頁面,而不需要Responsel.Redirect("");
            //System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text, false);  
        }

        else
        {
            Response.Write("用戶不合法");
        }      
    }
}

Step 5:拖一個Button 到 Default.aspx 上,將其text 屬性設為"登出",其事件代碼如下:

Button 事件代碼
protected void Button1_Click(object sender, EventArgs e)
    {
        System.Web.Security.FormsAuthentication.SignOut();
    }

http://m.survivalescaperooms.com/yoyebina/archive/2006/12/03/580121.html


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 文山县| 德兴市| 盐池县| 伊川县| 浏阳市| 珠海市| 遵化市| 广州市| 古蔺县| 安平县| 建宁县| 郁南县| 龙陵县| 安顺市| 夹江县| 石门县| 乳山市| 旌德县| 宜川县| 许昌市| 抚顺市| 中卫市| 大城县| 阳春市| 阜平县| 孝昌县| 应用必备| 常德市| 丰台区| 张家港市| 河源市| 如东县| 和林格尔县| 武定县| 略阳县| 溧阳市| 讷河市| 綦江县| 鄂尔多斯市| 团风县| 福清市|