Asp.net C#中如何给论坛加上cookies功能?
1个回答
展开全部
<asp:CheckBox ID="cbSave" runat="server" Text="保存密码" /><asp:DropDownList
ID="ddlExpir" runat="server">
<asp:ListItem Text="1天" Value="1"></asp:ListItem>
<asp:ListItem Selected="True" Text="1周" Value="7"></asp:ListItem>
<asp:ListItem Text="1月" Value="1"></asp:ListItem>
<asp:ListItem Text="永远" Value="365"></asp:ListItem>
</asp:DropDownList>
//////////////////后台代码//////////////////////
CookieManager.WriteCookie(Page, "LoginName",
strEmail, blSave, DateTime.Now.AddDays(day));
CookieManager.WriteCookie(Page, "PassWord",
strPwd, blSave, DateTime.Now.AddDays(day));
/////////////////////
方法:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// CookieManager 的摘要说明
/// </summary>
public class CookieManager
{
public CookieManager()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 取得指定名称的cookie
/// </summary>
/// <param name="strName"></param>
/// <returns></returns>
public static string GetCookie(Page page, string strName)
{
if (((page.Request.Cookies != null) &&
(page.Request.Cookies["book"] != null)) &&
(page.Request.Cookies["book"][strName] != null))
{
string value = page.Request.Cookies["book"][strName].ToString(); // 解密
return DESEn_Decrypt.Instance().Decrypt(value);
}
return "";
}
/// <summary>
/// 保存cookie
/// </summary>
/// <param name="strName"></param>
/// <param name="strValue"></param>
/// <param name="saveCookie"></param>
/// <param name="expires"></param>
public static void WriteCookie(Page page, string strName, string strValue, bool saveCookie, DateTime expires)
{
HttpCookie cookie = page.Request.Cookies["book"];
if (cookie == null)
{
cookie = new HttpCookie("book");
}
//cookie.Values[strName] = HttpUtility.UrlEncode(strValue);
strValue = DESEn_Decrypt.Instance().Encrypt(strValue); //加密
cookie.Values.Add(strName, strValue);
if (saveCookie)
{
cookie.Expires = expires;
}
page.Response.AppendCookie(cookie);
}
public static void ValidCookie(Page page)
{
HttpCookie cookie = page.Request.Cookies["book"];
if (cookie == null)
{
cookie = new HttpCookie("book");
}
cookie.Expires = DateTime.Now.AddDays(-1);
page.Response.AppendCookie(cookie);
}
}
ID="ddlExpir" runat="server">
<asp:ListItem Text="1天" Value="1"></asp:ListItem>
<asp:ListItem Selected="True" Text="1周" Value="7"></asp:ListItem>
<asp:ListItem Text="1月" Value="1"></asp:ListItem>
<asp:ListItem Text="永远" Value="365"></asp:ListItem>
</asp:DropDownList>
//////////////////后台代码//////////////////////
CookieManager.WriteCookie(Page, "LoginName",
strEmail, blSave, DateTime.Now.AddDays(day));
CookieManager.WriteCookie(Page, "PassWord",
strPwd, blSave, DateTime.Now.AddDays(day));
/////////////////////
方法:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// CookieManager 的摘要说明
/// </summary>
public class CookieManager
{
public CookieManager()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 取得指定名称的cookie
/// </summary>
/// <param name="strName"></param>
/// <returns></returns>
public static string GetCookie(Page page, string strName)
{
if (((page.Request.Cookies != null) &&
(page.Request.Cookies["book"] != null)) &&
(page.Request.Cookies["book"][strName] != null))
{
string value = page.Request.Cookies["book"][strName].ToString(); // 解密
return DESEn_Decrypt.Instance().Decrypt(value);
}
return "";
}
/// <summary>
/// 保存cookie
/// </summary>
/// <param name="strName"></param>
/// <param name="strValue"></param>
/// <param name="saveCookie"></param>
/// <param name="expires"></param>
public static void WriteCookie(Page page, string strName, string strValue, bool saveCookie, DateTime expires)
{
HttpCookie cookie = page.Request.Cookies["book"];
if (cookie == null)
{
cookie = new HttpCookie("book");
}
//cookie.Values[strName] = HttpUtility.UrlEncode(strValue);
strValue = DESEn_Decrypt.Instance().Encrypt(strValue); //加密
cookie.Values.Add(strName, strValue);
if (saveCookie)
{
cookie.Expires = expires;
}
page.Response.AppendCookie(cookie);
}
public static void ValidCookie(Page page)
{
HttpCookie cookie = page.Request.Cookies["book"];
if (cookie == null)
{
cookie = new HttpCookie("book");
}
cookie.Expires = DateTime.Now.AddDays(-1);
page.Response.AppendCookie(cookie);
}
}
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家可靠的代理服务提供商,提供原生IP(住宅原生IP)和高匿名代理服务。以下是关于StormProxies的原生IP服务的一些信息:1. 住宅原生IP:StormProxies提供的住宅原生IP是指从真实的家庭或企...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询