Free! ASP.NET Cookie Login
The article teaches you how to create a user login using the Dreamweaver MX ASP.NET/VB or C# server models. It also shows you how to protect a page and logout a user.
I don't think there is any article online that shows users how to create a login using the ASP.NET server model so enjoy and spread the word!!
This article was originally published on DMXFire and is copyright Omar Elbaga, reproduced with kind permission.
ASP.NET Cookie Login
Create dmx login form, dataset and validate it
Note: The code in this article is for documents that use the ASP.NET/VB or C# server models.
1. Create a new asp.net/vb page and name it login.aspx.
2. Create a login form with 2 asp:textboxes and 1 asp:button. Use the asp.net web server controls from Insert > ASP.NET Objects. Make sure the controls are within the form tag (dmx should do this by default.)
For the username textbox:
ID = txtusername
Text mode = SingleLine
For the password textbox:
ID = txtpassword
Text mode = Password
For the button:
ID = btn_login
Text = Login
Now let's create a DataSet to retrieve the users stored in the users table. We have to filter this dataset by the username and password that will be submitted by the login form we created for the user above.
1. Application Panel > Server Behaviors > + > DataSet
2. Name your dataset ds_checkuserinfo.
3. For the SQL select your user id, username and user password columns from the users table. Filter by the username and user password columns. The highlighted text in the image below are the names of 2 parameters that will reflect the values of the 2 textboxes.
Here is my SQL. Obviously, change the names of the columns and table to reflect your own, but do not change the parameter names (that are highlighted in the image):
SELECT user_id, user_name, user_pwd
WHERE user_name = @user_name AND user_pwd = @userpwd
Now let's add the parameters which will reflect the values submitted in the 2 textboxes of the form, one which is named txtusername and the other txtpassword.
1. From the Parameters section, click the plus (+) sign.
Name the parameter @user_name. Change the Type to WChar. Click the Build button.
2. Name the build value txtusername and get the source from the Form Variable and click OK. (This is using the value of whatever is submitted in the txtusername textbox as the value of the parameter.)
Now we want to add the second parameter, which will be the password.
1. Do the same as in Step 3 but in the Add parameter dialog box change the name of the parameter to @userpwd and in the build dialog box change the name to txtpassword. Â Click OK to close the Add Parameter dialog box.
2. Click OK to close the DataSet dialog box.
Now let's create the source code that will actually validate the DataSet. The source code has to find out whether a record was found in that DataSet we created and create a cookie if so, and if not, just reload the page.
1. Switch to Code view and add the following code above the <MM:dataset code.
Sub button_click(s as object, e as eventargs)
If ds_checkuserinfo.RecordCount = 1 ThenÂ
Response.Cookies("ckusername").Value = Request.Form("txtusername")
Response.Cookies("ckusername").Expires = DateTime.Now.AddDays(1)
Here's the C# code:
void button_click(Object s, EventArgs e)
if (ds_checkuserinfo.RecordCount == 1)
Response.Cookies["ckusername"].Value = txtusername.Text;
Response.Cookies["ckusername"].Expires = DateTime.Now.AddDays(1);
The code above checks to see whether the dataset named ds_checkuserinfo returned a record. If it does, that means the username and password submitted are valid; thus we create a cookie named ckusername using the value of the username and redirect to login1.aspx page. You can redirect to any page you like.
User ReviewsTotal of 2 reviews
Must declare the variable '@user_name'.Written by azzaliya Arief on June 25, 2004
Hi, I follow exactly the same steps,but why this error occurs? System.Data.OleDb.OleDbException: Must declare the variable '@user_name'. at System.Data.OleDb.OleDbDataReader.ProcessResults(Int32 hr) at System.Data.OleDb.OleDbDataReader.NextResult() ...
Article doesn't use .NET's Forms AuthenticationWritten by omar elbaga on June 11, 2004
Dear readers, Realize that this article doesn't use 'forms authentication', which would allow you to have the 'web.config' secure pages automatically. The article can be tailored fairly easily to include this though. Check back here or at ...