我正在登录页面上使用Remember Me
实施checkbox
功能。
当我登录帐户时,创建的cookie会过期!
这是我的整页代码
这是我的login.aspx.cs
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 && sn.LM_Username == username
select sn).ToList();
if (query.Count > 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);
}
}
这是我调用cookie的第二页
Dashboard.aspx.cs 这是我登录后的主页。
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 && Session["LoggedIn"] == null)
{
strRedirect = "~/LTS_Login.aspx";
Response.Redirect(strRedirect);
}
}
}
答案 0 :(得分:0)
我不确定你是否真的在重定向到Response.Redirect("~/Admin/UserHomePage.aspx");
后检查Cookie?所以我做了一个小例子来演示如何设置和检索cookie。
ASPX页面
<div>
Cookie:
<br />
<asp:Literal ID="litCookie" runat="server"></asp:Literal>
<br />
<br />
<asp:Button ID="btnSetCookie" runat="server" Text="Set Cookie"
onclick="btnSetCookie_Click" />
</div>
代码
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie c = Request.Cookies["myCookie"];
if (c != null)
litCookie.Text = string.Format("Username {0} Passwort {1}", c["username"], c["password"]);
else
litCookie.Text = "No Cookie \"myCookie\" found!";
}
protected void btnSetCookie_Click(object sender, EventArgs e)
{
HttpCookie c = new HttpCookie("myCookie");
c.Expires = DateTime.Now.Add(new TimeSpan(0, 1, 0));
c.Values.Add("username", "franz");
c.Values.Add("password", "1234");
Response.Cookies.Add(c);
litCookie.Text = "Cookie added! Please reload to load cookie";
// or redirect manually
// Response.Redirect("YOURSITE.aspx");
}