How to Implement Login Controls in ASP.NET Application With Example

By
Introduction :-  You already Know ,Internet is emerging as the most widely used medium for performing various tasks,such as online shopping ,Data exchanging and bank transactions etc. All this Information and data need to be secured against unauthorized access from malicious and illegal sources . For this purpose, we use authentication and authorization process. You can learn more from below about authentication and authorization in asp.net application.
You already know, we need to write large piece of code to create forms and user interfaces for authenticating the user and displaying the desired page based on the roles or rights given to the user.But It is very time consuming so that Microsoft developed a new series of server controls ,called login controls.
   To use login controls in your website. You just need to drag and drop them on the web page.
There is no need to write more codes in the codes-behind file.The Login controls have built in functionality for authentication and authorization of users.Microsoft were introduced (started) this services with ASP.NET 2.0. This control help to build the registration and login application without writing any code-behind codes and database. 
The Membership Service
This membership services is an important  feature of ASP.NET that helps you validatng and storing user credentials.
The ASP.NET Membership services helps to implement the folllowing functionalities in an application.
  • To create new user and password
  • To store the membership information ,such as username ,password .address,email and supporting data
  • To authenticate and authorization the visitors of the websites.
  • It allows the user to create and reset the password 
  • It allows to create a unique Identification system for authenticated users
The Login Controls:-
There are following Login controls developed by the Microsoft which are used in ASP.NET Website as given below:-
  1. Login
  2. LoginView
  3. LoginStatus
  4. Loginname
  5. PasswordRecovery
  6. ChangePassword
  7. CreateUserWizard 
      1.) The Login Control:-
The Login control provides a user interface which contains username and password, that authenticate the usernaMe and password and grant the access to the desired  services on the the basis of the credentials.
  There are used some methods ,properties and events in this Login control,You can check manually after drag and drop this control on your web form as given below:-

login page

     2.)  The LoginView Control:-
The LoginView Control is a web server control ,Which is used to display  two different views of a web page of any website , dependending on whether the any user has logged on to a web page as anonymous user or registered user .If the user is authenticated,the control displays the appropriate to the person with the help of the following views template.
  • Anonymous Template :-   This template (default of all) will be displayed when any user just open the web page but not logged in.
  • LoggedInTemplate:- This Template (page)will be displayed when the user in logged in.
  • RoleGroups:- This template will be displayed when user logged in, that is the member of the specific role (defined role group).
You can drag and drop Loginview Control on the web page from toolbox as shown below:-
loginview

Note:- You can do so, by adding any server controls such as Label ,Hyperlink and TextBox, to the empty region on the loginview control. 

     3.) The LoginStatus Control :-
The LoginStatus control specifies whether  a particular user has logged on the website or not . When the user is not logged in,this control display the Login text as a hyperlink.When the user is logged in ,this control display the logout text as a hyperlink.
   To do this ,Login Status control uses the authentication section  of the web.config file.
This control provides the following two views:-
  1. LoggedIn --> It will be displayed,when the user is not Logged In.
  2. Logout --> It will be displayed when the user is Logged In.
You can drag and drop this control on the web page as shown below:-

logistatus

     4.) The LoginName Control :-
The LoginName Control is responsible for display the names of all the authenticated users.If no users id logged in ,then this control is not displayed on the page.
This control uses the page .User.Identity.Name namespace to return the value for the user's name.
You can drag and drop Login Name control on the page from the toolbox as given below:-
login name

     5.) Passwordrecovery Control:-
 The Passwordrecovery control is used to recover or reset the forgotten password of a user . This control does not display the password on the browser,but it sends the password to the respective email address whatever you have specified at the time of registration.
This control has included three views as given below:-
  1. UserName :- It refers to the view that accepts the username of a user.
  2. Question :- It accepts the security questions asked from the users.
  3. Success :- It display a message to the user that retrieved password has been set to the user.
You can easily drag and drop this control on the web a page as shown below.


forget password

Note :-
  • To retrieve and reset password you must set some properties inside the asp.net Membership services.
  • You can can learn its methods ,properties and events from PasswordRecovery class yourself.
     6. ) CreateUserWizard control:-
This control uses the membership service to create a new user in the membership data store.The CreateUserWizard control is provides by the CreateUserWizard class and can be customized by using template and style properties .Using this control any user can easily create an account  and login to the web page.
You can drag and drop CreateUserWizared control on the web page as shown below:-
wizard

     7.) The ChangePassword Control:-
Using this control ,user can easily change your existing password (old password) on the ASP.NET Website.This control prompts uses to provide the current password first and then set the new password first and then set the new password.If the old password is not correct then new password can't be set. This is also helps to send the email to the respective users about the new password.This control is used ChangePassword class.
changepassword

There are some steps to use Login controls concepts in ASP.NET Application as given below:-
Step 1 :- First open your visual studio -->File -->New -->Select ASP.NET Empty website --> OK -->Open Solution Explorer -->Add a New web form (login.aspx) -->Now drag and Drop Login control and and LoginView control on the page from toolbox --> Add a Hyperlink control in LoginView 's blank space as shown below:-
login page

Step 2 :- Now open Solution Explorer --> Add a New web Form (Registrationpage.aspx) --> Drag and drop CreateUserWizard and LoginView controls on the page  --> Put a Hyperlink control inside blank space in LoginView control as shown below:-

  • Now select Complete from createUserWizard Tasks as shown above --> Now double click on Continue button and write the following c# codes for Navigation as given below:-
complet page

c# codes:-

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Registrationpage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ContinueButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("login.aspx");
    }
}

NOTE :- You can set this Navigation URL from the properties of Continue button also instead of this above codes. 
Step 3 :- Now Add a New Web Form (welcomwpage.aspx) --> drag and drop LoginName,LoginStatus and LoginView Controls on the page from toolbox as shown below:-
welcome page
 Step 4 :- Now Add again a New Web Form (changepassword.aspx)-->drag and drop ChangePassword control on the page from toolbox as shown below:-
changepassword

Step 5 :- Now Add a New web form (PasswordRecovery.aspx) -->drag and drop Passwordrecovery control from toolbox as shown below:-
recovery page

Step 6 :- Now open web.config file and write the following codes as given below:-

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
 <system.web>
    <authentication mode="Forms">
      <forms loginUrl ="login.aspx"
      defaultUrl = "Welcomepage.aspx" />
    </authentication>
    <authorization>
      <allow users="*"/>
    </authorization>
  <compilation debug="true" targetFramework="4.0"/>
 </system.web>
</configuration>


Step 7 :- Now Run the Application (Press F5) --> Now create a account first for access the website --> Press Create User button --> After that Press Continue button as shown below:-
fill registration
 Step 8 :- After step 7, login.aspx page will be opened --> Now put login credentials such as username and password --> Press login button --> You will see the following output as shown below:-


Note :-
  • If you want to change  old password , you can change it.I have already built it in this application. 
  • If you want to recover your password then you can dot it also.
  • You can download this whole application from below and run it on your visual studio.
  • In Next tutorial , I will put administrative and role based security in this application,First learn this whole concepts otherwise you can face  some problems in next tutorial.
  • You can  also customize this application according to you . 
For More...
Please share this application with your Friends also... 
Download Attached file
Download

0 comments:

Post a Comment

Powered by Blogger.