ASP. NET 2.0 writes a simple login using the stored procedure! Hope it will be helpful for beginners!
Posting time: 15:33:28 Author: li520will
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
{
Private readonly string sqlcon = configurationmanager. etettings ["constring"]. tostring ();
Protected void page_load (Object sender, eventargs E)
{
}
Protected void button#click (Object sender, eventargs E)
{
If (textbox1.text. Trim () = "" | textbox2.text. Trim () = "")
{
Response. Write (@ "<script language = 'javascript '> alert ('user name and password cannot be blank! ') </SCRIPT> ");
}
Else
{
Sqlconnection con = new sqlconnection (sqlcon );
Sqlcommand comm = con. createcommand ();
Comm. commandtype = commandtype. storedprocedure;
Comm. commandtext = "validatelogin ";
Comm. Parameters. addwithvalue ("@ username", textbox1.text. Trim ());
Comm. Parameters. addwithvalue ("@ password", textbox2.text. Trim ());
Sqlparameter sqlpal = comm. Parameters. addwithvalue ("@ rerurn_value ","");
Sqlpal. Direction = parameterdirection. returnvalue;
Con. open ();
Comm. executenonquery ();
Con. Close ();
Int iret = (INT) Comm. Parameters ["@ rerurn_value"]. value;
If (iret = 1)
{
Response. Write (@ "<script language = 'javascript '> alert ('login successful! Click "OK"); </SCRIPT> ");
Formsauthentication. redirectfromloginpage (textbox1.text. Trim (), true );
Response. Redirect ("default2.aspx ");
}
Else if (iret = 2)
{
Response. Write (@ "<script language = 'javascript '> alert ('your user name and password are incorrect. Please try again! '); </SCRIPT> ");
}
Else if (iret = 3)
{
Response. Write (@ "<script language = 'javascript '> alert (' sorry, you have not registered yet! '); </SCRIPT> ");
}
}
}
}
Certificate ------------------------------------------------------------------------------------------------------------------------------------------
Create a table with one ID, username, and password for each of the three fields.
The following is the stored procedure!
Alter procedure DBO. validatelogin
@ Username varchar (50 ),
@ Password varchar (50)
As
If @ username in (select Username
From logininfo)
Begin
If exists (select ID, username, password
From logininfo
Where (username = @ username) and (Password = @ password ))
Begin
Print 'login successful! '
Return 1
End
Print 'your username and password are incorrect. Please try again! '
Return 2
End
Print 'You have not registered yet! '
Return 3
Return
Certificate ---------------------------------------------------------------------------------------------------------------------------------------------