Note that there are some explanatory texts on larger screens.

plurals
  1. POCookie get expire after redirect in c#
    primarykey
    data
    text
    <p>I am implementing <code>Remember Me</code> functionality, on login page with a <code>checkbox</code>. When i logged into account cookie created get expire!<br> <i>this is my full page code</i></p> <p><b>this is my login.aspx.cs</b></p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using LTS_DAL; using System.Web.Security; public partial class LTS_Login : System.Web.UI.Page { LTS_DataClassesDataContext dc; HttpCookie c = new HttpCookie("remme"); public int ValidateUser(string username, string password) { int val=0; dc = new LTS_DataClassesDataContext(); var query = (from sn in dc.LTS_Login_Masters where sn.LM_Password == password &amp;&amp; sn.LM_Username == username select sn).ToList(); if (query.Count &gt; 0) { var qu = (from sn in dc.LTS_Employee_Masters where sn.Emp_ID == query[0].LM_Emp_ID select sn).ToList(); LTS_Session.Current.loginDetail = qu[0]; c.Values.Add("Emp_Name", qu[0].Emp_FName); c.Values.Add("Emp_ID", qu[0].Emp_ID.ToString()); Session["LoggedIn"] = "Yes"; var appoff = (from sn in dc.LTS_Approval_Officers where sn.Officer_1 == query[0].LM_Emp_ID || sn.Officer_2 == query[0].LM_Emp_ID select sn).ToList(); if (appoff.Count() != 0) { val = 2; } else { val = 1; } } return val; } protected void AuthenticateUser(string UserName, string Password, bool RememberMeSet) { string strRedirect = ""; int val = ValidateUser(UserName, Password); if (val == 1 || val == 2) { if (RememberMeSet) { //clear any other tickets that are already in the response Response.Cookies.Clear(); //set the new expiry date - to thirty days from now DateTime expiryDate = DateTime.Now.AddDays(30); //create a new forms auth ticket FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, UserName, DateTime.Now, expiryDate, true, String.Empty); //encrypt the ticket string encryptedTicket = FormsAuthentication.Encrypt(ticket); //create a new authentication cookie - and set its expiration date HttpCookie authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); authenticationCookie.Expires = ticket.Expiration; //add the cookie to the response. Response.Cookies.Add(authenticationCookie); c.Values.Add("UserName", UserName); c.Values.Add("Password", Password); Response.Cookies["remme"].Expires = DateTime.Now.AddHours(1); c.Expires = DateTime.Now.AddHours(1); } if (val == 1) { strRedirect = Request["ReturnUrl"]; if (strRedirect == null) strRedirect = "UserHomePage.aspx"; } else if (val == 2) { c.Values.Add("App_Off1", "y"); strRedirect = "~/Admin/DashBoard/DashBoard.aspx"; } Response.Cookies.Add(c); Response.Redirect(strRedirect); } else { lblMsg.Text = "Invalid Login"; } } protected void btnsubmit_Click(object sender, EventArgs e) { AuthenticateUser(txtusname.Text, txtpass.Text, chkRemme.Checked); } } </code></pre> <p><br> <br> <i> this is my second page where i am calling the cookie </i> <b>Dashboard.aspx.cs</b> this is my home page after login.</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using LTS_BAL; using System.Net.Mail; using System.Configuration; public partial class Admin_DashBoard_DashBoard : System.Web.UI.Page { Dash_Board em = new Dash_Board(); HttpCookie ck; static int Leave_ID; static int Emp_ID; protected void grdPenLeave_PageIndexChanging(object sender, GridViewPageEventArgs e) { grdPenLeave.PageIndex = e.NewPageIndex; BindGrdPenLeave(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ck = Request.Cookies.Get("remme"); Authorize(); } } protected void Authorize() { string strRedirect; if (Request.Cookies["remme"] != null) { if (Session["LoggedIn"] == null) { //if (Request.Cookies["remme"].Name[3] != null) if (ck["App_Off1"] != null) { strRedirect = "~/Admin/DashBoard/DashBoard.aspx"; Emp_ID = int.Parse(ck["Emp_ID"].ToString()); } else { strRedirect = Request["ReturnUrl"]; if (strRedirect == null) strRedirect = "UserHomePage.aspx"; } Response.Redirect(strRedirect); } else { Session["LoggedIn"] = "Yes"; if (ck["Emp_Name"] != null) { lblHeadUserName.Text = "Hello " + ck["Emp_Name"].ToString(); Emp_ID = int.Parse(ck["Emp_ID"].ToString()); } } } else if (Request.Cookies["remme"] == null &amp;&amp; Session["LoggedIn"] == null) { strRedirect = "~/LTS_Login.aspx"; Response.Redirect(strRedirect); } } } </code></pre> <p><br> </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload