Monday, June 25, 2012

Login with Session and Master Page/Web control

Lot's of my friends asking me that, they are all trouble with  login controls using in asp.net with session..? how it possible with master pages and web controls..? how cookies related with login controls..?

This article, i gonna be explain how all these possible in a simple manner... as an asp.net developer i needed to create a secure login web form but i also needed it to be a easy to implement, not so complicated, and also accomplish the mission
Session are a real impotent thing, that used in many cases we can't even imagine. You can use sessions in passing variables between Webpages instead of query-strings when passing secure values, it can also be used to create secure, and easy login.Sessions can be used to store even complex data for the user just like cookies. Actually sessions will use the cookies for store the data,unless you explicitly tell it not to. Sessions can be used easily in ASP.NET with the Session object. We will re-use the cookie example, and use sessions instead. Keep in mind though, that sessions will expire after a certain amount of minutes, as configured in the web.config file
Here, i need to explain all circumstances behind a login with a web user control.for that i can explain how a simple login works..!it can be detailed thru following program

Download sample program
Step 1 :
At first, set your master page [site.master]  with a web user control [LoginHeader.ascx] the path that i can  used for web control as


<%@ Register Src="~/LoginControls/LoginHeader.ascx" TagName="LoginHeader" TagPrefix="Uc1" %>

All the files such as webforms, database [Register.mdf], that are needed for this program are shown in solution explorer
Most Probably, the .ascx files contain link button and loginview controls and that can access to all web forms  through a masterpage [site.master] 
before that take a look for, how the design parts and code behind works in the .ascx file
LoginHeader.ascx


LoginHeader.ascx.cs































In .cs file convert all the cookie value into a session.. its not obviously termed as 'Convert', get the values from cookies and define all other links like register/edit
 Once the user is authenticated,.. he/she can view the profile and the edit page
Step 2:
before going to LoginHeader.ascx, it may check all users are authorized or not through a web form           [ Login.aspx]
Login.aspx

Login.aspx.cs
here it can assign  the cookie values for session 




Step 3:

For all these program, it has to define the connection string for its database [Register.mdf] and a registration form for inserting new clients

You can Download whole program Here

Now our code is ready for testing. Run your Application with Home.aspx as a start page 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

"Congratulations...!your session is starts..!"

In this 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. 

Thats all enJoY proGraMminG...!


Thank you for visiting http://rocingshani.blogspot.in
Shanith thekkayil-  techshaan2@gmail.com



2 comments:

  1. Hi Shanith

    Pages above are somewat blurred.... I can't read..


    Ameera


    http://appswin.blogspot.in/

    ReplyDelete
  2. @ameera:
    u just click on d codes and save as images in ur dsktop..... they r not codes actually...
    or u can dwnload d sample program from abve mentioned link

    ReplyDelete

Twitter Bird Gadget