SHARE
TWEET

CSharp Login

Ortund Jun 11th, 2013 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Login.aspx.cs
  2. namespace stman
  3. {
  4.     public partial class Login : System.Web.UI.Page
  5.     {
  6.         protected void btnLogin_Click(object sender, EventArgs e)
  7.         {
  8.             if (Page.IsValid)
  9.             {
  10.                 processLogin();
  11.             }
  12.         }
  13.  
  14.         protected void processLogin()
  15.         {
  16.             LoginContext log = new LoginContext();
  17.  
  18.             if (log.DoLogin(txtEmail.Text, txtPassword.Text))
  19.             {
  20.                 FormsAuthentication.SetAuthCookie(txtEmail.Text, True);
  21.                 Response.Redirect("Members/Default.aspx");
  22.             }
  23.         }
  24.     }
  25. }
  26.  
  27. // App_Code/LoginContext.cs
  28. namespace stman
  29. {
  30.     public class LoginContext
  31.     {
  32.         public bool DoLogin(string email, string password)
  33.         {
  34.             bool result = false;
  35.             string cmdText = "Users_SelectByEmailAndPassword";
  36.            
  37.             List<MySqlParameter> args = new List<MySqlParameter>();
  38.             args.Add(new MySqlParameter("@email", MySqlDbType.VarChar));
  39.             args[args.Count - 1].Value = email;
  40.             args.Add(new MySqlParameter("@password", MySqlDbType.VarChar));
  41.             args[args.Count - 1].Value = password;
  42.  
  43.             DataTable dt;
  44.  
  45.             // Database is just a class that handles the database queries and returns the data
  46.             using (Database db = new Database(cmdText, CommandType.StoredProcedure, args))
  47.             {
  48.                 dt = db.GetDataTable();
  49.             }
  50.  
  51.             if (dt != null)
  52.             {
  53.                 populateSessionVars(dt.Rows[0]);
  54.                 result = true;
  55.             }
  56.  
  57.             return result;
  58.         }
  59.  
  60.         private void populateSessionVars(DataRow row)
  61.         {
  62.             HttpContext.Current.Session["userid"] = row["userid"];
  63.             HttpContext.Current.Session["emailaddress"] = row["email"];
  64.             HttpContext.Current.Session["firstname"] = row["firstname"];
  65.             HttpContext.Current.Session["lastname"] = row["lastname"];
  66.         }
  67.     }
  68. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top