asp.Net操作クッキーの簡単な例
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
HttpCookie cookie = Request.Cookies["loginInfo"];
if (cookie != null)
{
cb_repwd.Checked = true;
tb_loginName.Text = UIHelper.Decrypt(cookie["loginName"]);
tb_pwd.Attributes.Add("value", UIHelper.Decrypt(cookie["pwd"]));
}
else
{
tb_pwd.Attributes.Add("value", "******");
}
}
}
protected void btn_login_Click(object sender, EventArgs e)
{
if (UIHelper.GetString(Session["Code"]).ToUpper()!=this.tb_code.Text.Trim().ToUpper())
{
oJavaScript.Alert(" ");
return;
}
NetUserService NerService = ContextFactory.GetObject("NetUserService");
string sql = string.Format("from NetUser where LoginName='{0}'", tb_loginName.Text.Trim());
IList list = NerService.Find(sql);
if (list.Count > 0)
{
if (list[0].Status == 2)
{
oJavaScript.Alert(" , ");
return;
}
}
if (NerService.Login(tb_loginName.Text.Trim(), oSecurity.GetPwdEncrypt(tb_pwd.Text.Trim())))
{
HttpCookie cookie = Request.Cookies["loginInfo"];
if (cb_repwd.Checked)
{
if (cookie == null)
{
HttpCookie cookielogin = new HttpCookie("loginInfo");
setCookies(cookielogin,7);
}
else
{
if (UIHelper.Decrypt(Request.Cookies["loginInfo"]["loginName"]) != tb_loginName.Text.Trim())
{
setCookies(cookie, 7);
}
}
}
else
{
setCookieExpires(cookie);
}
oJavaScript.Redirect("/");
}
else
{
oJavaScript.Alert( " ");
}
}
#region cookie
///
/// cookie
///
/// HttpCookie cookielogin
///
private void setCookies(HttpCookie cookielogin, int Expires)
{
cookielogin.Values["loginName"] = UIHelper.Encrypt(tb_loginName.Text.Trim());
cookielogin.Values["pwd"] = UIHelper.Encrypt(tb_pwd.Text.Trim());
cookielogin.Expires = DateTime.Now.AddDays(Expires);
Response.AppendCookie(cookielogin);
}
///
/// cookie
///
/// HttpCookie cookielogin
private void setCookieExpires(HttpCookie cookielogin)
{
if (cookielogin != null)
{
cookielogin.Expires = DateTime.Now.AddDays(-1); ;
Response.AppendCookie(cookielogin);
}
}
#endregion
クッキーの期限切れを設定するのは、実はクッキーを修正することです.クライアントにクッキーを再送信する必要があります.
Request.Cookies cookie,response,cookies追加cookie
簡単ですが、よく分からないことは間違いやすいです.