ViewState with example in asp.net C#

Tuesday, December 14, 2010

How to make secure web application

Step1->The best way to add logout button in your master page instead of each page in your application
  
Demonstration:- <asp:LinkButton ID="lnk" runat="server" Text="Logout" ForeColor="white"
                onclick="lnk_Click" CausesValidation="false"></asp:LinkButton>

Step2-> Now remove the session from cache, To do it, write the following code in page_load event of your master page in which you have logout button.

Demonstration:-

                           protected void Page_Load(object sender, EventArgs e)
                            {
                                     Response.Cache.SetCacheability(HttpCacheability.NoCache);
                             }

Step3-> Now write the following code in click event of link button

Demonstration:-
                         protected void lnk_Click(object sender, EventArgs e)
                                   {
                                       Session.Abandon();      
                                        Response.Redirect("~/Home.aspx");
                                   }

After performing all these steps your logout button will perform fine,
If you want to make more secure of your web application then you need to add Global.asax file in your web application. We define an event in this file that will perform globally and provide security from intruders.

Step1-> *Right click on your web application .
             *Add new item.
             *Add Global.asax file.

Step2-> Write a function in your Global.asax file as mention below.

                       void session_start()
                            {
                              if (Session["usr"] == null)
                                {
                                     Response.Redirect("~/Home.aspx");
                                    //Here you can write the address of your home page or index page
                                    // Or which you want to make start page.
                                 }
                          }


Now your web application is secure. If any one  paste the url of  any  page of your web application in the browser but browser will show only home or index page of your web application insteade of appropriate page. This is because your Global.asax file that works globally.
<a herf=" http://aspnet-with-c-sharp.blogspot.com/"> Logout button in your web application</a>
    

1 comment: