/** * * 保存TOKEN信息 * */ public void saveToken() { string token_id = GuidUtils.getGuid(); string md5 = MD5Utils.getMD5(token_id); this.ViewData[Constant.TOKEN_ID_FORM] = "<input type=\"hidden\" name=\"" + Constant.TOKEN_ID_FORM + "\" value=\"" + md5 + "\">"; // this.Session[Constant.TOKEN_ID_SESSION] = md5; HttpCookie token_session = new HttpCookie(Constant.TOKEN_ID_SESSION); token_session["token_session"] = md5; token_session.Expires = DateTime.Now.AddMinutes(20); Response.Cookies.Add(token_session); } /** * * 验证FORM 内的TOKEN信息 是否与SESSION的信息一致 * */ public bool isValidateToken() { // string md5 = this.Session[Constant.TOKEN_ID_SESSION] as string; HttpCookie token_session = System.Web.HttpContext.Current.Request.Cookies.Get(Constant.TOKEN_ID_SESSION); string md5 = Convert.ToString(token_session["token_session"]); string md5_form = this.read(Constant.TOKEN_ID_FORM); if (md5.Equals(md5_form)) { return true; } else { return false; } }
时间: 2024-10-09 08:41:22