C#使用cookie记住密码

 

 记住密码:(Cookie)

第一步:在加载页面里面先定义一个Cookie对象

 HttpCookie cookie = CookieHelper.GetCookie("UserPass");

   //判断Cookie是否为空

if (cookie != null)

 {

  //  把UserPass值转换给哪个类型的UserInfoCookie

System.Collections.Specialized.NameValueCollection UserInfoCookie = Request.Cookies["UserPass"].Values;

 

  this.UserName.Text =System.Web.HttpUtility.UrlDecode(UserInfoCookie["Uname"]);//转为编码格式

 

 //不能直接把密码为Password的文本框直接赋值  

解决办法为  txtPassword.Attributes["value"] = "123"

 this.UserPass.Attributes["value"] = UserInfoCookie["Upass"];    }   

第二步:

  //判断复选框是否被选中

   if (this.RemPass.Checked)

 {

//实例化Cookie的值

 HttpCookie Cookie = CookieHelper.GetCookie("UserPass");

  {

  if (Cookie == null)

 {

 Cookie = new HttpCookie("UserPass");

  // cookie.Values.Add("loginname", System.Web.HttpUtility.UrlEncode(User_Name));//增加属性

 Cookie.Values.Add("Uname", System.Web.HttpUtility.UrlEncode(User_Name)); //转为Utf_8格式

Cookie.Values.Add("Upass", PassWord);

  //设置Cookie过期时间

Cookie.Expires = DateTime.Now.AddMinutes(1);

CookieHelper.AddCookie(Cookie);

  }

 else     //cookie不为空

if(!Cookie.Values["Uname"].Equals(User_Name)&& !Cookie.Values["Upass"].Equals(PassWord))

   {

   CookieHelper.SetCookie("UserPass", "Uname", User_Name);

  CookieHelper.SetCookie("UserPass", "Upass", PassWord);

    }

第三步:

CookieHelp

       /// <summary>

        /// 获取cookie的值

        /// </summary>

        /// <param name="CookieName"></param>

        /// <returns></returns>

        public static string GetCookieValue(string CookieName)

        {

            return GetCookieValue(CookieName);

        }

 

        /// <summary>

        /// 获得cookie的值

        /// </summary>

        /// <param name="CookieName"></param>

        /// <param name="key"></param>

        /// <returns></returns>

        public static string GetCookieValue(string CookieName, string key)

        {

            HttpRequest request = HttpContext.Current.Request;

            if (request != null)

                return GetCookieValue(request.Cookies[CookieName].ToString(), key);

            return null;

        }

        /// <summary>

        /// 获得Cookie的子键值

        /// </summary>

        /// <param name="cookie"></param>

        /// <param name="key"></param>

        /// <returns></returns>

        public static string GetCookieValue(HttpCookie cookie, string key)

        {

            if (cookie != null)

            {

                if (!string.IsNullOrEmpty(key) && cookie.HasKeys)

                    return cookie.Values[key];

                else

                    return cookie.Value;

            }

            return "";

        }

 

        /// <summary>

        /// 获得Cookie

        /// </summary>

        /// <param name="cookieName"></param>

        /// <returns></returns>

        public static HttpCookie GetCookie(string cookieName)

        {

            HttpRequest request = HttpContext.Current.Request;

            if (request != null)

                return request.Cookies[cookieName];

            return null;

        }

 

        /// <summary>

        /// 设置Cookie子键的值

        /// </summary>

        /// <param name="cookieName"></param>

        /// <param name="key"></param>

        /// <param name="value"></param>

        public static void SetCookie(string cookieName, string key, string value)

        {

            SetCookie(cookieName, key, value, null);

        }

 

        /// <summary>

        /// 设置Cookie值

        /// </summary>

        /// <param name="key"></param>

        /// <param name="value"></param>

        public static void SetCookie(string key, string value)

        {

            SetCookie(key, null, value, null);

        }

 

        /// <summary>

        /// 设置Cookie值和过期时间

        /// </summary>

        /// <param name="key"></param>

        /// <param name="value"></param>

        /// <param name="expires"></param>

        public static void SetCookie(string key, string value, DateTime expires)

        {

            SetCookie(key, null, value, expires);//设置过期时间

        }

 

        /// <summary>

        /// 设置Cookie过期时间

        /// </summary>

        /// <param name="cookieName"></param>

        /// <param name="expires"></param>

        public static void SetCookie(string cookieName, DateTime expires)

        {

            SetCookie(cookieName, null, null, expires);

        }

        /// <summary>

        /// 设置Cookie

        /// </summary>

        /// <param name="cookieName"></param>

        /// <param name="key"></param>

        /// <param name="value"></param>

        /// <param name="expires"></param>

        public static void SetCookie(string cookieName, string key, string value, DateTime? expires)

        {

            HttpResponse response = HttpContext.Current.Response;

            if (response != null)

            {

                HttpCookie cookie = response.Cookies[cookieName];

                if (cookie != null)

                {

                    if (!string.IsNullOrEmpty(key) && cookie.HasKeys)//是否具有主键

                        cookie.Values.Set(key, value);

                    else

                        if (!string.IsNullOrEmpty(value))

                            cookie.Value = value;

                    if (expires != null)

                        cookie.Expires = expires.Value;

                    response.SetCookie(cookie);

                }

            }

 

        }

 -----------------------------cookieHelper

public class CookieHelper
{
/// <summary>
/// 获取cookie的值
/// </summary>
/// <param name="CookieName"></param>
/// <returns></returns>
public static string GetCookieValue(string CookieName)
{
return GetCookieValue(CookieName);
}

/// <summary>
/// 获得cookie的值
/// </summary>
/// <param name="CookieName"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string GetCookieValue(string CookieName, string key)
{
HttpRequest request = HttpContext.Current.Request;
if (request != null)
return GetCookieValue(request.Cookies[CookieName].ToString(), key);
return null;
}


/// <summary>
/// 获得Cookie的子键值
/// </summary>
/// <param name="cookie"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string GetCookieValue(HttpCookie cookie, string key)
{
if (cookie != null)
{
if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
return cookie.Values[key];
else
return cookie.Value;
}
return "";
}

/// <summary>
/// 获得Cookie
/// </summary>
/// <param name="cookieName"></param>
/// <returns></returns>
public static HttpCookie GetCookie(string cookieName)
{
HttpRequest request = HttpContext.Current.Request;
if (request != null)
return request.Cookies[cookieName];
return null;
}

 

#region 删除Cookie

/// <summary>
/// 删除Cookie
/// </summary>
/// <param name="cookieName"></param>
public static void RemoveCookie(string cookieName)
{
RemoveCookie(cookieName,null);
}

/// <summary>
/// 删除Cookie的子键
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
public static void RemoveCookie(string cookieName,string key)
{
HttpResponse response = HttpContext.Current.Response;
if (response != null)
{
HttpCookie cookie = response.Cookies[cookieName];
if (cookie != null)
{
if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
cookie.Values.Remove(key);
else
response.Cookies.Remove(cookieName);
}
}
}

#endregion
#region 设置/修改Cookie

/// <summary>
/// 设置Cookie子键的值
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void SetCookie(string cookieName, string key, string value)
{
SetCookie(cookieName, key, value, null);
}

/// <summary>
/// 设置Cookie值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void SetCookie(string key, string value)
{
SetCookie(key, null, value, null);
}

/// <summary>
/// 设置Cookie值和过期时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
public static void SetCookie(string key, string value, DateTime expires)
{
SetCookie(key, null, value, expires);
}

/// <summary>
/// 设置Cookie过期时间
/// </summary>
/// <param name="cookieName"></param>
/// <param name="expires"></param>
public static void SetCookie(string cookieName, DateTime expires)
{
SetCookie(cookieName, null, null, expires);
}

/// <summary>
/// 设置Cookie
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
public static void SetCookie(string cookieName, string key, string value, DateTime? expires)
{
HttpResponse response = HttpContext.Current.Response;
if (response != null)
{
HttpCookie cookie = response.Cookies[cookieName];
if (cookie != null)
{
if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
cookie.Values.Set(key, value);
else
if (!string.IsNullOrEmpty(value))
cookie.Value = value;
if (expires != null)
cookie.Expires = expires.Value;
response.SetCookie(cookie);
}
}

}

#endregion

#region 添加Cookie

/// <summary>
/// 添加Cookie
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void AddCookie(string key, string value)
{
AddCookie(new HttpCookie(key, value));
}

/// <summary>
/// 添加Cookie
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
public static void AddCookie(string key, string value, DateTime expires)
{
HttpCookie cookie = new HttpCookie(key, value);
cookie.Expires = expires;
AddCookie(cookie);
}

/// <summary>
/// 添加为Cookie.Values集合
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void AddCookie(string cookieName, string key, string value)
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Values.Add(key, value);
AddCookie(cookie);
}

/// <summary>
/// 添加为Cookie集合
/// </summary>
/// <param name="cookieName">Cookie名称</param>
/// <param name="expires">过期时间</param>
public static void AddCookie(string cookieName, DateTime expires)
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Expires = expires;
AddCookie(cookie);
}

/// <summary>
/// 添加为Cookie.Values集合
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expires"></param>
public static void AddCookie(string cookieName, string key, string value, DateTime expires)
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Expires = expires;
cookie.Values.Add(key, value);
AddCookie(cookie);
}

/// <summary>
/// 添加Cookie
/// </summary>
/// <param name="cookie"></param>
public static void AddCookie(HttpCookie cookie)
{
HttpResponse response = HttpContext.Current.Response;
if (response != null)
{
//指定客户端脚本是否可以访问[默认为false]
cookie.HttpOnly = true;
//指定统一的Path,比便能通存通取
cookie.Path = "/";
//设置跨域,这样在其它二级域名下就都可以访问到了
//cookie.Domain = "chinesecoo.com";
response.AppendCookie(cookie);
}
}

#endregion

}

posted @ 2018-09-02 14:14  逆水行舟-奕  阅读(997)  评论(0编辑  收藏  举报