asp.net
Articles on Forms validation online Chibai, but it took me 1 and a half days to learn "a little",
Now to share the code out, I hope for beginners like me all the help, but also hope that the master to give advice:
--------------------------------------------------------------------------------
Step 1: New Database (library: myforms; table: Users; fields: Id,username, userpwd);
Step 2: Create a new Web site, web.config the full code of the file is as follows:
All code for Web.config
<?xml version= "1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug= "true"/>
<sessionstate cookieless= "AutoDetect"/>
<!--solve--> when cookies are disabled on the browser side
<authentication mode= "Forms" >
<forms name= "CookieName" loginurl= "Login.aspx" protection= "All" ></forms>
<!--loginurl is the login face URL, and 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 a checkbox;
and set the checkbox's Text property to: "Save Cookis";
The hidden code for step 4:login.aspx is as follows:
Login All Hide Code
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 namespaces
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=.;D atabase=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, such as through the validation of the direct direction to the requested page, without the need for Responsel.redirect ("");
System.Web.Security.FormsAuthentication.RedirectFromLoginPage (this. TextBox1.Text, false);
}
Else
{
Response.Write ("User not lawful");
}
}
}
Step 5: Drag a button to the Default.aspx, set its Text property to "Log out", and its event code is as follows:
Button event code
protected void Button1_Click (object sender, EventArgs e)
{
System.Web.Security.FormsAuthentication.SignOut ();
}