You can try this code,
In the master Page load event write this code,
add a property
public bool m_bLoginRequired = true;
public bool IsLoginRequired
{
get { return m_bLoginRequired; }
set { m_bLoginRequired = value; }
}
try
{
// Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
Response.Cache.SetNoStore();
if (IsLoginRequired==true)
{
if ( Session.IsNewSession || HttpContext.Current.Session["Username"] == null)
{
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage("Session Expired");
Response.End();
}
}
}
catch (Exception ex)
{
throw (ex);
}
now in Login page you need to write this code
FormsAuthentication.SetAuthCookie(this.txt_UserName.Text.Trim(), false); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, this.txt_UserName.Text.Trim(), DateTime.Now, DateTime.Now.AddMinutes(10), false, "HR");
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
cookie.Name = "jay";
Session["UserName"] = txt_UserName.Text.Trim();
Response.Cookies.Add(cookie);
txt_UserName.Text = "";
txt_Password.Text = "";
Response.Redirect("HomePage2.aspx");
now you ave to add pageinit event in the login page
protected void Page_PreInit(object sender, EventArgs e)
{
Master.IsLoginRequired = false;
}
if you want that the user can access an un authorized page then
in the pageinit event of that page
set the Master.IsLoginRequired=false;
also specify the loginurl in the web.config file.