Tuesday, June 3, 2008

Asp .Net 2.0 Membership I - LoginStatus, Login and LoginView Controls





This is part I of a series of Membership blogs I am planning to post.
This blog is intended to the a supplement to my Youtube video Asp.Net 2.0 Membership I : LoginStatus LoginName.



Authentication and Authorization are the most important, time consuming and error prone task in a site development. Thanks to ASP .Net 2.0's membership options, the whole task is simplified.

There are there Authentication options available in Asp. Net :
1. Windows Authentication
2. Forms Authentication
3. Passport Authentication

Windows Authentication is used in small intranet sites, where the the authentication process is tied to Corporate Active directory. This is the default authentication type in ASP .Net.

Forms Authentication (Web Forms Authentication) use a Database to store user credentials and is approach used in public facing website that we encounter on a daily basis. This blog concentrates on Forms Authentication.

Passport Authentication is an authentication option provided by Microsoft, whereby Microsoft handles the whole authentication process. This option is mainly found in Microsoft family of websites.

In this blog I am going to concentrate on Forms Authentication.


Asp .Net comes packed a number of Login controls as shown in the figure below.
















Go to Website and select ASP.Net Configuration. Web site administration tool will appear.




















Select security and select authentication type, select From the Internet as the authentication type. Click Done. Now you are done with Forms Authentication.




















If you inspect the web.config the Authentication mode has changed to forms and inside the App_Data a new database ASPDBNETDB.MDF is created.

Click on Create User in Web Site Administration tool.
Create user form will appear. Here you expected to give a strong password which is more than 7 characters in length and contain at least one special character.

LoginStatus Control
Now come back to Default.aspx and place a LoginStatus control. LoginStatus Control acts as a toggle control control and redirects you to the login page.



















Create a new page called Login.aspx. Place a Login Control. Now when you run the application and click on loginstatus control you will be taken to the login page. On successful submission of user name and password you will be redirected back to the default page. Other wise you can't proceed as show in the figure.



















LoginName Control

All the sites provides on option seeing the user name. This can be accomplished using LoginName control. So place a LoginName control on default.aspx and again follow the same login process on successful login you will see the login name.

If you want a greeting message like Hello prefixed on the user name you can use the FormatString property of LoginName control like this : Hello...{0}.

LoginView Control
Using LoginView Control you can have custom contents for Anonymous and Logged In Users.
For this LoginView Control is having two templates Anonymous Template and Logged In Template.

Anonymous Template is the default template where you can specify the contents for anonymous users.




















In LoggedIn Template you can specify the contents that can be viewed by logged in users. In this example I am adding the guitar chords of Hotel California.

The following figure shows the Anonymous template in action.













The following figure shows the Logged in template in action.


















Related Blogs

aspnet_regsql to port Membership API database to Sql Server



Happy Programming
Shalvin.com

1 comment:

  1. Very nice tutorial and pictures. Now I'll finally be able to figure out a login scheme for our tiny intranet here!!

    ReplyDelete