Last post Jun 04, 2012 05:27 AM by kyberias
Apr 21, 2008 08:28 AM|bilal_hru|LINK
Will anyone tell me how to Authenticate Users with the SQL Server database table using C# with Example?
Membership API controls for Login and Registering Users for authentication,
but i need/require to store Username/Password in a DatabaseTable(SQL SERVER table) in Register.aspx and then Match them against the DB table in Login.aspx page.Plz forward the two files.
I'm doing having great difficulty to accomplish that, Plz anyone provide me with the "Complete Example" Easy to understand and simple code
to authenticate against a DB table
Apr 21, 2008 01:43 PM|srulyt|LINK
check this out
Apr 22, 2008 10:05 PM|acit|LINK
hi there. you may try these codes.(sorry, its in vb)
blnResult = SiteSpecificAuthenticationMethod(Login1.UserName, Login1.Password)
reader = con2.ExecuteReader()
thats the codes i used for my system. hope that it'll help you.
Jun 27, 2008 01:35 PM|zeeca|LINK
I'm new here. And i would like to say thanks to acit about his post I would translate that in C#.
As i belive there are lots of other people who wants in C# and most of the places provide too complecated codes.
This one is the simplest and to the point. I just converted it into C#. Please see below setp by step.
Create a page with the name of login.aspx
Place login control on it. It's default id will be login1
Create a another with name main_page.aspx (this page will be you home or default page)
Codding for login.aspx.cs file
_Default : System.Web.UI.Page
sender, EventArgs e)
// Check if the user is already loged in or not
!= null) && (Convert.ToBoolean(Session["Check"])
// If User is Authenticated then moved to a main page
sender, AuthenticateEventArgs e)
blnresult = false;
// Pass UserName and Password from login1 control to an authentication function which will check will check the user name and password from sql server.
// Then will retrun a true or false value into blnresult variable
blnresult = Authentication(Login1.UserName, Login1.Password);
// If blnresult has a true value then authenticate user
if (blnresult ==
// This is the actual statement which will authenticate the user
e.Authenticated = true;
// Store your authentication mode in session variable
// If user faild to provide valid user name and password
// Function name Authentication which will get check the user_name and passwrod from sql database then return a value true or false
username, string password)
sqlstring = "Select user_name, password from [user_table] where user_name='"
+ username + "' and password ='" + password +
// create a connection with sqldatabase
con = new System.Data.SqlClient.SqlConnection(
Data Source=datebaseservername;Initial Catalog=datebasename;UserID=databaseusername;Password=databasepassword;Connect Timeout=10;TrustServerCertificate=True "
// create a sql command which will user connection string and your select statement string
System.Data.SqlClient.SqlCommand comm =
// create a sqldatabase reader which will execute the above command to get the values from sqldatabase
// open a connection with sqldatabase
// execute sql command and store a return values in reade
reader = comm.ExecuteReader();
// check if reader hase any value then return true otherwise return false
coding need to be added in web.config this code will redirect a user to login.aspx page if user is not logged in and also restrict anonymous users.
If you need more help you can e-mail me at
firstname.lastname@example.org or visit www.zeeshanfaisal.com
Sep 06, 2008 08:26 AM|mysterenet|LINK
thank you a lot acit for your post, it will help me to advance in my project, but i have a problem is that the type of 'System.Data.SqlClient.SqlDataReadercon1' is not defiened. do you have any idea if i must download a framework or how to solve the problem?
Your help will make me advance more in my project. thanks in advance
Sep 09, 2008 10:31 PM|acit|LINK
basically the 'system.data.sqlclient.sqldatareadercon1' was to define the connection string that the system will connect to.
i want to simplify my code therefore i used the existed connection string which is 'sqldatasource1'.
there's no need for you to download a framework or anything.
you only need to do a few modification to your codes only.
Oct 22, 2008 06:29 PM|Quakerjacks|LINK
Data Source=ServerName;Initial Catalog=Database;UserID=userid;Password=passwordi;Connect Timeout=10;TrustServerCertificate=True);
When I run the project this the error I get -->
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1026: ) expected
Line 53: // create a connection with sqldatabase
Line 54: System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(
Line 55: Data Source=Confirm;Initial Catalog=IMB;UserID=sa;Password=mmi;Connect Timeout=10;TrustServerCertificate=True);
Line 57: // create a sql command which will user connection string and your select statement string
Line 53: // create a connection with sqldatabase
Here is what my project is - I have a SQL user database that I would like to use to Authenticate Users to login into my wesite, after they login I need to run a few start procedures based upon a user.
Oct 23, 2008 12:58 AM|zeeca|LINK
First i want to thank you. Because you found a bug/error in my code.
And i apologies for any inconvenience.
It's a small mistake you just need to add double quotes before and after the connection string.
See the following correct code.
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(
" Data Source=Confirm;Initial Catalog=IMB;UserID=sa;Password=mmi;Connect Timeout=10;TrustServerCertificate=True
In the above string you can see quotes in red color before and after the connection string that's all you need to put in ur code and you are good to go.
I also fixed my above code.
feel free to ask if you have any problem.
Oct 24, 2008 04:05 PM|Quakerjacks|LINK
Your Awsome thanks!!!![:D]
Can I run something by you?? This is kind of new to me I am a Java/PhP guy. So this is what I am trying to accomplish I have a web server running SQL Server 2005 with a database and a user table.
I need to create a login webpage using the user name and password from the database and then run a SQL Start procedure based upon a user in the table.
Sorry I know it’s kind of long but was wondering if you could point me in the right direction????
Oct 24, 2008 05:18 PM|zeeca|LINK
As i understand you are already able to authenticate user. Now u want to call a stored procedure from sql database for that user.
Read the following article. It explains how to call sql procedure.
Jun 02, 2009 02:20 PMemail@example.com|LINK
i have used this code and this is working. But i have another link for sign up and it should redirect to the page for completing sign up but this link is not working.please give me a solution for this.
Jun 03, 2009 11:52 PM|zeeca|LINK
You can use following code by using link Button to redirect to your login page.
Sep 01, 2009 10:34 PM|supersyed|LINK
Hi acit i've used your code aswell, it was exactly what I was looking for, but I got the same error as mysterynet. I havent created a database as yet though, would creating one solve my problem?
Sep 02, 2009 08:02 AM|mysterenet|LINK
hiiiiiiii if i still rememeber because i don't use asp no more. you choose where you have dropdownlist...., you choose
sqldatasource. you put it in your graphic. you make the connection and it works automatically. i hope to help you even if i'm begginer in this field
Sep 02, 2009 09:46 AM|supersyed|LINK
Thanks a lot, i think u've just solved my problem
Sep 02, 2009 09:58 AM|mysterenet|LINK
you're welcome, that's make me happy that the problem was solved , good programing
Nov 11, 2010 07:28 AM|rootuid|LINK
I'm having a problem with the c sharp example specifically the line
blnresult = Authentication(Login1.username, Login1.Password);
The compiler tells me "The name 'Login1" does not exist in the current context". I'm using VS2010. On login aspx I've added a login control called Login1 so it should be seen. Any ideas?
Nov 11, 2010 01:40 PM|zeeca|LINK
Just double click on your Login1 control and then copy this code inside that function. If still doesn't work send me an email i'll send you a sample project.
Mar 13, 2011 04:14 PM|zia ur rehman|LINK
thanks a lot sir........
Jun 04, 2012 05:27 AM|kyberias|LINK
WARNING, DON'T USE THIS CODE ANYWHERE! It has a trivial but HUGE
SQL injection vulnerability. See method Authentication and how you're forming the SQL statement. Never pass user input straight to SQL statements!
See this for more information: