This blog explains how to create a login page in ASP .Net using C#. The login uses MS SQL authentication.
1. From the Toolbox, drag a Login control from the Login tab to the aspx page.
2. Select the Login control. Go to Properties window, choose events, click on Authenticate, then press Enter. The window will redirect you to the C# coding page with the authenticate event.
3. Add the following code to Login1_Authenticate event.
*Login1 is the name of the Login control
*Login1.Username gets the text entered on the username textbox, same with the Login1.Password which gets the text entered on the password textbox.
4. Add the ValidateLogin method:
*This contains the query to read from your database, given that you have a Users table, username and password columns.
5. The whole code should look like this:
*Take note to reference System.Web and System.Data assemblies.
*The SqlConnection initializes your connection to your database.
6. On the Web.Config file, add the following:
*On the authentication tag, defaultUrl is the landing page after you logged in; loginUrl is the name of the login page.
*I added a defaultDocument because my demo was created from an empty website; I set the defaultDocument to my login page, which is Login.aspx.
No comments:
Post a Comment