Hi…Here am going to demonstrate a simple asp.net application which focus on authentication and authorization code of C#.Net. Lets begin our journey…
1. Create a new ASP.Net Empty Project using C# language and name it as “User_Roles”.
2. Create a new ASP Folder by right clicking a solution and name it as “MembersArea”.
3. Add a new aspx page named “Login.aspx” and drag drop Login control from toolbox on to the form,double click on it which auto generates eventhandler and code the .cs file as shown below:
Here I hard Coded the username as “aejaaz” and password as”12345″. If the end user enters these credentials then the method AuthenticateUser returns TRUE or else FALSE which is assigned to member variable “ismember” in conditional statement based on which it redirect to respective page.
4. Open the web.config file of the root and code as shown in image as:
Here we are implementing the authentication to our app by opting mode of authentication as “Forms” and providing DefaultUrl and LoginUrl..
5. Add a web.config file and a new webform named as “member.aspx” to the MembersArea Folder.
6. Code the web.config file as shown below:
In the code we are restricting users who are considered as anonymous which is depicted by “?” in Authorization tag. Simple–The resouces/pages in this “MembersArea” directory are being restricted to anonymous users but allow to other register users.
7. Design the Default.aspx as shown in below image as:
Here “Members Page” is a hyperLink to which set a NavigateUrl property as “~/Members_Area/member.aspx”
8. Set Default.aspx page as StartUp Page and Design the Member.aspx page with a simple message as “Welcome!!! From MembersArea Directory..members.aspx page”…Thats all our journey is end…
Observation: At first the Default.aspx page gets loaded and when you click the “Members Page” link it redirects to Login.aspx page and prompts for Credentials to enter, if you enter correct credentials then it redirects to member.aspx page whose screen shots are shown as:
Step B: Click the Members Page hyperlink which need to be redirected to login.aspx page as:
Step C: Enter correct Credentials and submit which redirects to member.aspx page which is intentional.
Just one little thing…Drag-drop a label on to the page and add the following code to page_load event of member.aspx page as shown: