There are hundreds of articles online about Forms validation, but it took me a day and a half to learn "a little bit".
Now I share the code, hoping it will be helpful to beginners like me, and I also hope experts can give me some pointers:
-------------------------- -------------------------------------------------- ----
Step 1: Create a new database (library: MyForms; table: users; fields: ID, userName, userPwd);
Step 2: Create a new website. The entire code of the web.config file is as follows:
All code of web.config
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="true"/>
<sessionState cookieless="AutoDetect"/>
<!--Solution when cookies are disabled on the browser-->
<authentication mode="Forms">
<forms name="CookieName" loginUrl="login.aspx" protection="All"></forms>
<!--loginUrl is the login page URL. If there is no authentication cookie, the client will be redirected to this URL-->
</authentication>
<authorization>
<deny users="?"/>
</authorization>
<customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
</system.web>
</configuration>
Step 3: Add a login.aspx page; drag 2 TextBox, 1 Button and 1 CheckBox;
And set the text attribute of CheckBox to: "Whether to save Cookies";
Step 4: The hidden code of login.aspx is as follows:
login All hidden codes
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; //Import namespace
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");
//The above two lines can also be replaced by the following line. If the verification is passed, it will be directed to the requested page without the need for Response.Redirect("");
//System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text, false);
}
else
{
Response.Write("User is illegal");
}
}
}
Step 5: Drag a Button to Default.aspx, set its text attribute to "Logout", and its event code is as follows:
Button event code
protected void Button1_Click(object sender, EventArgs e)
{
System.Web.Security.FormsAuthentication.SignOut();
}
http://www.cnblogs.com/yoyebina/archive/2006/12/03/580121.html