0
我正在爲客戶開發登錄和用戶數據庫系統。我遇到的問題是我試圖註冊用戶設備,試圖限制某個特定用戶在一個月內可以使用哪些設備登錄。這一切都工作得很好,但我在客戶端計算機保存cookie的問題。Cookie消失
即使即時設置expired如下面的代碼所示,Internet Explorer刪除會話結束時的cookie。它在Chrome中運行得很好。 設置cookie的代碼:
cookie = new HttpCookie("DeviceLog");
cookie.Expires = DateTime.Now.AddDays(30);
cookie.Values.Add("DeviceId", guid.ToString());
Response.Cookies.Add(cookie);
Retrive餅乾代碼:
HttpCookie cookie = Request.Cookies["DeviceLog"];
我承認我對餅乾knowlege是limitet,所以它的propperly東西相當obivius,但我havnt能夠解決它,甚至p
更新,整個cookie的代碼片段:
protected void Page_Load(object sender, EventArgs e) {
if (User.Identity.IsAuthenticated) {
Response.Redirect(STSUtility.GetRefereUrl(true));
}
if (!IsPostBack) {
try {
if (Request.Cookies["UserName"] != null && Request.Cookies["Password"] != null) {
username.Text = Request.Cookies["UserName"].Value;
password.Attributes["value"] = AESEncrypter.Decrypt(Request.Cookies["Password"].Value);
remember.Checked = true;
}
} catch (Exception) {
Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);
Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);
}
}
}
protected void btnLogin_Click(object sender, EventArgs e) {
if (remember.Checked) {
Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(30);
Response.Cookies["Password"].Expires = DateTime.Now.AddDays(30);
} else {
Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);
Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);
}
Response.Cookies["UserName"].Value = username.Text.Trim();
Response.Cookies["Password"].Value = AESEncrypter.EncryptToken(password.Text.Trim());
MembershipProvider mp = new MembershipProvider();
bool userValidated = mp.ValidateUser(username.Text, password.Text);
if (userValidated && Page.IsValid) {
HttpCookie cookie = Request.Cookies["DeviceLog"];
ContextDataContext model = new ContextDataContext();
User user = model.Users.First(x => x.Email == username.Text.Trim());
Session["UserId"] = user.Id.ToString();
int userDevices = model.UserDevicesLogs.Count(x => x.UserId == user.Id);
if (!user.Active) {
return;
}
if (userDevices >= maxDeviceLoginsPrUser && cookie == null) {
Response.Redirect("/Sites/Login/ExceedMaxDevices.aspx");
}
if (cookie == null) {//Hvis device ikke har en cookie
Guid guid = Guid.NewGuid();
cookie = new HttpCookie("DeviceLog");
cookie.Expires = DateTime.Now.AddDays(30);
cookie.Values.Add("DeviceId", guid.ToString());
Response.Cookies.Add(cookie);
model.UserDevicesLogs.InsertOnSubmit(new UserDevicesLog {
Id = Guid.NewGuid(),
UserId = user.Id,
DeviceId = guid,
LastLoginFromDevice = DateTime.Now
});
} else {//Hvis device har en cookie
Guid deviceId;
Guid.TryParse(cookie.Values["DeviceId"], out deviceId);
UserDevicesLog deviceLog = model.UserDevicesLogs.FirstOrDefault(x => x.UserId == user.Id && x.DeviceId == deviceId);
if (deviceLog == null) {
model.UserDevicesLogs.InsertOnSubmit(new UserDevicesLog {
Id = Guid.NewGuid(),
DeviceId = deviceId,
UserId = user.Id,
LastLoginFromDevice = DateTime.Now
});
} else {
deviceLog.LastLoginFromDevice = DateTime.Now;
}
Response.Cookies.Add(cookie);
}
model.SubmitChanges();
FormsAuthentication.SetAuthCookie(username.Text, remember.Checked);
if (String.IsNullOrEmpty(Request.QueryString["ReturnUrl"])) {
Response.Redirect(STSUtility.GetRefereUrl(true));
} else {
FormsAuthentication.RedirectFromLoginPage(user.Email, remember.Checked);
}
}
}
谷歌上一小時後
這僅僅是DeviceLog餅乾那給了我這個問題......
它只是工作在Chrome瀏覽器中沒問題,但Internet Explorer和FireFox將cookie作爲會話cookie來對待,而不是設置30天過期日期。 – MazZ 2014-10-08 08:34:06
剛剛測試了你的代碼,你的代碼工作正常,我可以從IE瀏覽器中讀取cookie,我正在使用IE 11 – User2012384 2014-10-08 08:38:50
你想發佈一些更多的代碼嗎?例如代碼在頁面加載,頁面初始化和一些在母版頁等 – User2012384 2014-10-08 08:39:48