我正在为客户开发登录和用户数据库系统。我遇到的问题是,我正在尝试注册用户设备,以试图限制给定用户在一个月内可以使用的多恶意设备。这一切都工作得很好,但我在客户端计算机上保存的cookie存在问题。
即使我设置了expiredate,如下面的代码所示,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);
回复cookie代码:
HttpCookie cookie = Request.Cookies["DeviceLog"];
我承认我对cookie的了解是限制性的,所以它是相当客观的,但是我已经能够解决它,即使在Google上花了一个小时后: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 cookie才能解决这个问题......
答案 0 :(得分:0)
好的,我自己解决了这个问题,最后......
我有Response.Cookies.Add(cookie);代码中的twise,这是原因。不知道为什么这是一个问题,因为它是相同的cookie添加twise,并且是...愚蠢的愚蠢我知道愚蠢:)感谢sudgestions,我很高兴终于有这个解决:)