Page authentication on web pages

Page authentication in web pages




Web.config :

<system.web>
    
<compilation debug="true" targetFramework="4.0"/>
   
   
     <authentication mode="Forms">
     <forms loginUrl="Default.aspx" name="login" timeout="120"></forms>
     </authentication>
   
     <authorization>
          <deny users="?"/>
     </authorization>
   
   

</system.web>



Login.aspx :


protected void btnLogin_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
        SqlDataAdapter adp = new SqlDataAdapter("select*from tbl_registration where username = '"+txtid.Text.Trim()+"' and password = '"+txtpassword.Text.Trim()+"'",con);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            FormsAuthentication.SetAuthCookie(txtid.Text.Trim(), false);
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            lblmessage.Text = "Wrong ID and Password.";
            lblmessage.ForeColor = System.Drawing.Color.Red;
        }
       
    }


Default.aspx :

protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));     Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
    }
protected void btnLogOut_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        FormsAuthentication.SignOut();
        Response.Redirect("Login.aspx");
    }


note:
1. This code is disable caching on individual pages that are sensitive or volatile can be done with code such as follows :

Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));     Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();


// for more detail about authentication and authorization 
//visit here... 
http://www.codeproject.com/Articles/98950/ASP-NET-authentication-and-authorization#5%20steps%20to%20enable%20authentication%20and%20authorization%20using%20Windows

Comments

Popular posts from this blog

Tree view in winforms using c#

how to Replace null value with 0 Using C#

how to fetch all HTML Table Records and modify all records accordingly using jquery and Javascript