![]() |
|
[.NET] [Asp.Net] How to Write a Simple login page in Asp.net - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [.NET] [Asp.Net] How to Write a Simple login page in Asp.net (/showthread.php?tid=2479914) |
[Asp.Net] How to Write a Simple login page in Asp.net - globemasters - May 9, 2012 Hello HF Today i am going to share with you a simple tutorial How to write a simple login page using Asp.Net Using the code We are going to user C# as our language. Start Open Visual Studio and Create a New Website. Automatically you will have an empty page defined for you like this Code <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>Step: 1 Go to Design View and you will notice there is nothing on your page. Now open your Toolbox and add buttons and some textbox for username and password. Code <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>And your Design Looks like this; ![]() Now as you can see our login page is created, Let us see how we can validate the login and what is needed to have a proper login. Open your Sql management Studio and Create a New Database, but if you already have it you will just follow the Step 2 where we add a table. Step 2: Create a Database & Create a Table Code CREATE TABLE [dbo].[Log_Users]Step 3: Let us Add Sample Data Code insert into dbo.Log_UsersIn the above Stored Procedure we count the Records that have matched the Records and if there is one record found then it is a good login else it is a bad login. But how will you use this in your asp.net Page. First we have to create a Function that will access the stored procedure and call that function in click event of the button. Create a Function as show below in your page not inside your page load because you will get an Error. Code public int Validate_Login(String Username, String Password)As you can see this Function return an Integer, as we said before this will return either a 1 which is equal to “Good” and other numbers will be “Bad”. The login Data should be clean, no Duplicates should be there because this will break your functionality. It might return the duplicates and the count might not match the if statement that you will see later in this article. Double click you Button and add the following code in the Click event of the Button. Code protected void btnlogin_Click(object sender, EventArgs e)Now our code is ready for testing. Run your Application and enter an incorrect password deliberately and see what message you see and when you enter the correct login you will receive a message that says Login is Good, Send the User to another page or enable controls In your application you can redirect the user to another page and store the Session that you will use through out your application And abondon when the user exit your application. Please note that for some application it is good to enable and disable Controls based on the Session value, meaning that you can check if the user is logged in , and display the benefits that logged in user can get in the same page. I will not explain more on that because it is beyong the scope of this article. Conclusion There are a lot of ways to do a login control in asp.net, but I thought it will be important to point the basic one to HF users who are interested in lear coding. Thanks for the time have good day. Even i am still a learner. |