heey, i am doing a project for school witch includes a login in asp.net with a SQL server database, i have a login name and a password in the database, i heard this was only possible using cookies, i have look for a week now and it should be ready the 22 of december 2011.
i have a query and i want the result to be the cookie if i try this each time i get the result -1 if i put the anwser in a GridView i get the good result.
this is the code on the Btn_Click as far as i have it:
protected void btnInloggen_Click(object sender, EventArgs e)
{
string sEMail = txtUserEMail.Text;
string sPassword = txtPassword.Text;
gvCookie.DataSource = SQL_codes.Inloggen(sEMail, sPassword);
gvCookie.DataBind()//this was to test my query;
HttpCookie cookUserName = new HttpCookie("Username");
cookUserName.Value = Convert.ToInt16(gvCookie);//thought something liek this would do it but it doesn't so what i want is the result of the query here
Response.Cookies.Add(cookUserName);
if (Request.Cookies["Username"] !=null)
{
string sName = Request.Cookies["Username"].Value;
Response.Write(sName);
}
}
and this is my how i think i should use my query:
public static DataTable Inloggen(string sEMail, string sPassword)
{
string sql = string.Format("select OpdrachtGever.opdrid from OpdrachtGever where OpdrachtGever.eMail = '{0}' and OpdrachtGever.Password = '{1}'", sEMail, sPassword);
SqlDataAdapter da = new SqlDataAdapter(sql, SQL_codes.GetConnection());
DataTable dt = new DataTable("reserveringen");
da.Fill(dt);
return dt;
}
(it’s in dutch because i am dutch sorry)
now i though maybe i can put the result into a variable but i can’t vindt how this works
it would be awesome if you guys(or girls) could help me because i am completly stuck
Cookies are used to for persistent data. Without cookies, a web server wouldn’t be able to tell one client from another.
Manually managing cookies can be messy, especially for stuff like logins. You can instead use session variables via ASP.NET, which abstracts away the management of cookies in a secure manner and lets you treat an object like an object.
Session[“blnIsLoggedIn”] = MethodValidateLogin(strUserName, strPassword);
Then check on page load if they are logged in
if(Session[“blnIsLoggedIn”] != null and (boolean)Session[“blnIsLoggedIn”] == true)
Do This
else
Do That
There are many more important things to know about for logins, like hashing and salting passwords, protecting against SQL Injections(parameterized inputs), and stuff. But it sounds like you’re just doing a beginner’s project, so I won’t explain them but leave you with the terms.
P.S. I don’t do much ASP.NET and what I do make is intranet only, so there may be better ways than session variables for tracking if a user is logged in