Você está na página 1de 3

Re: How to Authenticate Users with the SQL Server database table using C# with Example?

Jun 27, 2008 06:35 PM|LINK Hi There, 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. Step 1: 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) Step 2: Codding for login.aspx.cs file using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // Check if the user is already loged in or not if ((Session["Check"] != null) && (Convert.ToBoolean(Session["Check"]) == true)) { // If User is Authenticated then moved to a main page if (User.Identity.IsAuthenticated) Response.Redirect("main_page.aspx"); } } protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) { Boolean blnresult;

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 == true) { // This is the actual statement which will authenticate the user e.Authenticated = true; // Store your authentication mode in session variable Session["Check"] = true; } else // If user faild to provide valid user name and password e.Authenticated = false; } // Function name Authentication which will get check the user_name and passwrod from sql database then return a value true or false protected static Boolean Authentication(string username, string password) { string sqlstring; sqlstring = "Select user_name, password from [user_table] where user_name='" + username + "' and password ='" + password + "'"; // create a connection with sqldatabase System.Data.SqlClient.SqlConnection 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 = new System.Data.SqlClient.SqlCommand(sqlstring,con); // create a sqldatabase reader which will execute the above command to get the values from sqldatabase System.Data.SqlClient.SqlDataReader reader; // open a connection with sqldatabase con.Open(); // 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 if (reader.Read()) return true; else 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. <system.web> <authentication mode="Forms"> <forms loginUrl="login.aspx" name="login" protection="All"/> </authentication> <authorization> <deny users="?"/> </authorization> </system.web> If you need more help you can e-mail me at zeeskt74@hotmail.com or visit www.zeeshanfaisal.com Thanks

Você também pode gostar