//check whether a cookie is expired //if expired property is set, then just return it value //if not set, check whether is a session cookie, if is, then not expired //if expires is set, check its real time is expired or not public bool isCookieExpired(Cookie ck) { bool isExpired = false; if ((ck != null) && (ck.Name != "")) { if (ck.Expired) { isExpired = true; } else { DateTime initExpiresValue = (new Cookie()).Expires; DateTime expires = ck.Expires; if (expires.Equals(initExpiresValue)) { // expires is not set, means this is session cookie, so here no expire } else { // has set expire value if (DateTime.Now.Ticks > expires.Ticks) { isExpired = true; } } } } else { isExpired = true; } return isExpired; }
例 7.12. isCookieExpired 的使用范例
//extract cookies for upload file cookiesForUploadFile = new CookieCollection(); foreach (Cookie ck in skydriveCookies) { if ((ck.Domain == constDomainLiveCom) && (!commLib.isCookieExpired(ck))) { Cookie ckToAdd = new Cookie(ck.Name, ck.Value, ck.Path, ck.Domain); ckToAdd.HttpOnly = ck.HttpOnly; ckToAdd.Expires = ck.Expires; ckToAdd.Secure = ck.Secure; ckToAdd.Version = ck.Version; cookiesForUploadFile.Add(ckToAdd); } } //!!! if not seperatly set new domain value, then will overwirtten the original domain of cookie in skydriveCookies foreach (Cookie ckNew in cookiesForUploadFile) { ckNew.Domain = constDomainUsersStorageLive; }