/// <summary> /// 保存一个Cookie /// </summary> /// <param name="CookieName">Cookie名称</param> /// <param name="CookieValue">Cookie值</param> /// <param name="CookieTime">Cookie过期时间(小时),0为关闭页面失效</param> public static void SaveCookie(string CookieName, string CookieValue, double CookieTime) { HttpCookie myCookie = new HttpCookie(CookieName); DateTime now = DateTime.Now; myCookie.Value = DEncrypt.Encrypt(HttpUtility.HtmlEncode(CookieValue), "chinacloudtech"); if (CookieTime != 0) { //有两种方法,第一方法设置Cookie时间的话,关闭浏览器不会自动清除Cookie //第二方法不设置Cookie时间的话,关闭浏览器会自动清除Cookie ,但是有效期 //多久还未得到证实。 myCookie.Expires = now.AddMinutes(CookieTime); if (HttpContext.Current.Response.Cookies[CookieName] != null) { HttpContext.Current.Response.Cookies.Remove(CookieName); } HttpContext.Current.Response.Cookies.Add(myCookie); } else { if (HttpContext.Current.Response.Cookies[CookieName] != null) { HttpContext.Current.Response.Cookies.Remove(CookieName); } HttpContext.Current.Response.Cookies.Add(myCookie); } }
/// <summary> /// 跨域访问 /// </summary> /// <param name="url"></param> /// <param name="param"></param> /// <returns></returns> public static string Post(string url, string param, string tokenId = "", int time = 60000) { Uri address = new Uri(url); HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; request.Method = "POST"; request.ContentType = "application/json;charset=utf-8"; //"application/x-www-form-urlencoded"; request.Timeout = time; byte[] byteData = UTF8Encoding.UTF8.GetBytes(param == null ? "" : param); request.ContentLength = byteData.Length; if (!string.IsNullOrEmpty(tokenId)) { string sysId = System.Configuration.ConfigurationManager.AppSettings["SysId"]; string accessKey = System.Configuration.ConfigurationManager.AppSettings["AccessKey"]; accessKey = string.IsNullOrEmpty(accessKey) ? "" : accessKey; request.Headers.Add("Authorization", "Basic " + DEncrypt.Encrypt(sysId + DEncrypt.Encrypt(tokenId, accessKey), "")); } using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } string result = ""; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); result = reader.ReadToEnd(); } return(result); }
/// <summary> /// 取得CookieValue /// </summary> /// <param name="CookieName">Cookie名称</param> /// <returns>Cookie的值</returns> public static string GetCookie(string CookieName) { HttpCookie myCookie = new HttpCookie(CookieName); myCookie = HttpContext.Current.Request.Cookies[CookieName]; if (myCookie != null) { string cookievalue = DEncrypt.Decrypt(myCookie.Value, "chinacloudtech"); return(HttpUtility.HtmlDecode(cookievalue)); } else { return(null); } }
public static byte[] GetVerifyCode() { int codeW = 80; int codeH = 30; int fontSize = 16; string chkCode = string.Empty; //颜色列表,用于验证码、噪线、噪点 Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue }; //字体列表,用于验证码 string[] font = { "Times New Roman" }; //验证码的字符集,去掉了一些容易混淆的字符 char[] character = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' }; Random rnd = new Random(); //生成验证码字符串 for (int i = 0; i < 4; i++) { chkCode += character[rnd.Next(character.Length)]; } //写入Session、验证码加密 var md5str = DEncrypt.Get16_Md5Lower(chkCode.ToLower(), null); WebHelper.WriteSession("Login_VerifyCode", md5str); //创建画布 Bitmap bmp = new Bitmap(codeW, codeH); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.White); //画噪线 for (int i = 0; i < 3; i++) { int x1 = rnd.Next(codeW); int y1 = rnd.Next(codeH); int x2 = rnd.Next(codeW); int y2 = rnd.Next(codeH); Color clr = color[rnd.Next(color.Length)]; g.DrawLine(new Pen(clr), x1, y1, x2, y2); } //画验证码字符串 for (int i = 0; i < chkCode.Length; i++) { string fnt = font[rnd.Next(font.Length)]; Font ft = new Font(fnt, fontSize); Color clr = color[rnd.Next(color.Length)]; g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18, (float)0); } //将验证码图片写入内存流,并将其以 "image/Png" 格式输出 MemoryStream ms = new MemoryStream(); try { bmp.Save(ms, ImageFormat.Png); return(ms.ToArray()); } catch (Exception) { return(null); } finally { g.Dispose(); bmp.Dispose(); } }