You can try this code. In the main 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 on the 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");
you can now add the pageinit event on the login page
protected void Page_PreInit(object sender, EventArgs e) { Master.IsLoginRequired = false; }
if you want the user to be able to access an unauthorized page, then in the page event of this page
set Master.IsLoginRequired=false;
also specify loginurl in the web.config .
jimmy
source share