You are on page 1of 2

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 using using using using using using using using using System; System.Data; System.Configuration; System.Collections; System.Web; System.Web.Security; System.Web.UI; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; 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["Ch eck"]) == 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, AuthenticateEventAr gs e) { Boolean blnresult; blnresult = false; // Pass UserName and Password from login1 control to an authen tication function which will check will check the user name and password from sq l server. // Then will retrun a true or false value into blnresult variab le 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 th e 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 an d passwrod from sql database then return a value true or false protected static Boolean Authentication(string username, string passw ord) { 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.SqlCl ient.SqlConnection( " Data Source=datebaseservername;Initial Catalog=d atebasename;UserID=databaseusername;Password=databasepassword;Connect Timeout=10 ;TrustServerCertificate=True " ); // create a sql command which will user connection string and yo ur select statement string System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClien t.SqlCommand(sqlstring,con); // create a sqldatabase reader which will execute the above comm and 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 ret urn 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.as px 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>

You might also like