private void InitializeComponent()
{
thisbtnLogin Click += new System Web UI ImageClickEventHandler(this btnLogin_Click);
}
事件注冊好之後
private void btnLogin_Click(object sender
System Web UI ImageClickEventArgs e)
{
CCommonDB sql = new CCommonDB();
string redirect =;
if((redirect = sqlAuthenticateUser(this Session this Response
usernameText password Text saveLogin Checked)) != string Empty)
{
// Redirect the user
ResponseRedirect(redirect);
}
else
{
MessageText = Login Failed! ;
}
}
讀者看完上面的代碼之後一定想問CCommonDB是哪裡來的東東
CCommonDB
cs
namespace secureComponents
{
public class CCommonDB : CSql
{
public CCommonDB() : base() { }
public string AuthenticateUser(
SystemWeb SessionState HttpSessionState objSession // Session Variable
SystemWeb HttpResponse objResponse // Response Variable
string email// Login
string password// Password
bool bPersist // Persist login
)
{
int nLoginID =;
int nLoginType =;
// Log the user in
Login(emailpassword ref nLoginID ref nLoginType);
if(nLoginID !=) // Success
{
// Log the user in
SystemWeb Security FormsAuthentication SetAuthCookie(nLoginID ToString() bPersist);
// Set the session varaibles
objSession[loginID ] = nLoginID ToString();
objSession[loginType ] = nLoginType ToString();
// Set cookie information incase they made it persistant
SystemWeb HttpCookie wrapperCookie = new System Web HttpCookie( wrapper );
wrapperCookieValue = objSession[ wrapper ] ToString();
wrapperCookieExpires = DateTime Now AddDays( );
SystemWeb HttpCookie lgnTypeCookie = new System Web HttpCookie( loginType );
lgnTypeCookieValue = objSession[ loginType ] ToString();
lgnTypeCookieExpires = DateTime Now AddDays( );
// Add the cookie to the response
objResponseCookies Add(wrapperCookie);
objResponseCookies Add(lgnTypeCookie);
return/candidate/default aspx ;
}
case: // Admin Login
{
return/admin/default aspx ;
}
case: // Reporting Login
{
return/reports/default aspx ;
}
default:
{
return stringEmpty;
}
}
}
else
{
return stringEmpty;
}
}
/// <summary>
/// Verifies the login and password that were given
/// </summary>
/// <param name=>the login</param>
/// <param name=password >the password</param>
/// <param name=nLoginID >returns the login id</param>
/// <param name=nLoginType >returns the login type</param>
public void Login(string emailstring password ref int nLoginID ref int nLoginType)
{
ResetSql();
DataSet ds = new DataSet();
// Set our parameters
SqlParameter paramLogin = new SqlParameter(@username SqlDbType VarChar );
paramLoginValue = email;
SqlParameter paramPassword = new SqlParameter(@password SqlDbType VarChar );
paramPasswordValue = password;
CommandCommandType = CommandType StoredProcedure;
CommandCommandText = glbl_Login ;
CommandParameters Add(paramLogin);
CommandParameters Add(paramPassword);
AdapterTableMappings Add( Table Login );
AdapterSelectCommand = Command;
AdapterFill(ds);
if(dsTables Count != )
{
DataRow row = dsTables[ ] Rows[ ];
// Get the login id and the login type
nLoginID = ConvertToInt (row[ Login_ID ] ToString());
nLoginType = ConvertToInt (row[ Login_Type ] ToString());
}
else
{
nLoginID =;
nLoginType =;
}
}
}
abstract public class CSql
{
private SqlConnection sqlConnection; // Connection string
private SqlCommand sqlCommand; // Command
private SqlDataAdapter sqlDataAdapter; // Data Adapter
private DataSet sqlDataSet; // Data Setpublic CSql()
{
sqlConnection = new SqlConnection(ConfigurationSettingsAppSettings [
ConnectionString ]);
sqlCommand = new SqlCommand();
sqlDataAdapter = new SqlDataAdapter();
sqlDataSet = new DataSet();
sqlCommandConnection = sqlConnection;
}
/// <summary>
/// Access to our sql command
/// </summary>
protected SqlCommand Command
{
get { return sqlCommand; }
}
/// <summary>
/// Access to our data adapter
/// </summary>
protected SqlDataAdapter Adapter
{
get { return sqlDataAdapter; }
}
/// <summary>
/// Makes sure that everything is clear and ready for a new query
/// </summary>
protected void ResetSql()
{
if(sqlCommand != null)
{
sqlCommand = new SqlCommand();
sqlCommandConnection = sqlConnection;
}
if(sqlDataAdapter != null)
sqlDataAdapter = new SqlDataAdapter();
if(sqlDataSet != null)
sqlDataSet = new DataSet();
}
/// <summary>
/// Runs our command and returns the dataset
/// </summary>
/// <returns>the data set</returns>
protected DataSet RunQuery()
{
sqlDataAdapterSelectCommand = Command;
sqlConnectionOpen();
sqlConnectionClose();
sqlDataAdapterFill(sqlDataSet);
return sqlDataSet;
}
}
}
[
From:http://tw.wingwit.com/Article/program/net/201311/15688.html