为了账号安全,请及时绑定邮箱和手机立即绑定

请问asp.net 使用cookie保存用户信息和读取?

/ 猿问

请问asp.net 使用cookie保存用户信息和读取?

慕姐8265434 2019-06-25 19:15:12

asp.net 使用cookie保存用户信息和读取


查看完整描述

3 回答

?
浮云间

写出CookieResponse.Cookies["userName"].Value = "test";
Response.Cookies["userName"].Expires = DateTime.Now.AddMonths(1);读取验证CookieString userName = String.Empty;
if(Request.Cookies["userName"]!=null)
userName = Request.Cookies["userName"].Value;

查看完整回答
反对 2019-06-29
?
慕运维8079593

保存Cookies代码:
protected void btnSetCookiesClick(object sender, EventArgs e)
{
cookiesManagerTest1.AddSubkey(txtSetCookiesName.Text,txtSetCookiesValue.Text);
Response.Redirect("CookiesManagerGet.aspx");
}

读取Cookies代码:
protected void btnGetCookiesClick(object sender, EventArgs e)
{
HttpCookie cookies = Request.Cookies["ManagerTest"];
if (cookies.HasKeys)
{
if (!String.IsNullOrEmpty(cookies.Values[txtCookiesName.Text]))
{
txtGetCookies.Text = cookies.Values[txtCookiesName.Text];
}
}
}

其中cookiesManagerTest1是一个自定义用户控件的ID,
控件代码如下:
public class CookiesManager : WebControl
{
#region Private
private string _cookieName = "";
private string _cookieSubkey = "";
private string _cookieValue = "";
private bool _dataBound;
private DateTime _expireDate = DateTime.Now.AddDays(3650);
private HttpCookie _cookie;
#endregion
#region Properties
public string CookieValue
{
get { return GetCookieValue(); }
set { _cookieValue = value; }
}

public string CookieName
{
get { return _cookieName; }
set { _cookieName = value; }
}

public string CookieSubkey
{
get { return _cookieSubkey; }
set { _cookieSubkey = value; }
}

public DateTime ExpireDate
{
get { return _expireDate; }
set { _expireDate = value; }
}
#endregion

#region Contructors
public CookiesManager()
{
}

public CookiesManager(string cookiename, string cookiesubkey, string cookievalue, DateTime expiredate)
{
_cookieName = cookiename;
_cookieSubkey = cookiesubkey;
_cookieValue = cookievalue;
_expireDate = expiredate;
}
#endregion

#region Cookie Control event
/// <summary>
/// Setup the CookiesManager instance in the pages controlstate control collection
/// </summary>
/// <param name="e"> </param>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);

if (Page.Items[typeof(CookiesManager)] == null)
{
Page.Items[typeof(CookiesManager)] = this;
}

Page.RegisterRequiresControlState(this);
}

/// <summary>
/// In the control load, if the Cookie doesn't exist, then will CreateCookie
/// </summary>
/// <param name="e"> </param>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

if (!_dataBound)
{
DataBind();
}
if (!CookieExisted())
CreateCookie();
else
RenewCookie();
}

public override void DataBind()
{
base.DataBind();
_dataBound = true;
}
#endregion
#region Public Methods
public string GetCookieValue()
{
string tmpValue = "";
if (CookieExisted())
{
if (string.IsNullOrEmpty(CookieSubkey))
tmpValue = HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Cookies[CookieName].Value);
else
tmpValue = HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Cookies[CookieName][CookieSubkey]);
}
return tmpValue;
}

public string GetCookieValue(string cookieSubkey)
{
string tmpValue = "";
if (CookieExisted())
{
tmpValue = HttpContext.Current.Server.HtmlEncode(HttpContext.Current.Request.Cookies[CookieName][cookieSubkey]);
}
return tmpValue;
}

/// <summary>
/// this function remove a subkey, and will do nothing if the subkey doesn't exist
/// </summary>
/// <param name="subkeyname"> </param>
public void RemoveSubkey(string subkeyname)
{
if (CookieExisted())
{
_cookie = HttpContext.Current.Request.Cookies[CookieName];
if (_cookie.Values[subkeyname] != null)
_cookie.Values.Remove(subkeyname);
}
}

/// <summary>
/// this function adds a new subkey to the existing cookie, if the subkey already existed,
/// then the fucntion will update the value of the subkey
/// </summary>
/// <param name="subkeyname"> </param>
/// <param name="subkeyvalue"> </param>
public void AddSubkey(string subkeyname, string subkeyvalue)
{
if (CookieExisted())
{
_cookie = HttpContext.Current.Request.Cookies[CookieName];
if (_cookie.Values[subkeyname] == null)
_cookie.Values.Add(subkeyname,subkeyvalue);
else
_cookie.Values[subkeyname] = subkeyvalue;
_cookie.Expires = ExpireDate;
}
}

/// <summary>
/// this function set the cookie.Expires to the ExpireDate
/// </summary>
public void RenewCookie()
{
if (CookieExisted())
{
_cookie = HttpContext.Current.Request.Cookies[CookieName];
_cookie.Expires = ExpireDate;
}
}

/// <summary>
/// this function removes a cookie with the list of cookies with the specific CookieName
/// </summary>
public void DeleteCookie()
{
if (CookieExisted())
{
_cookie = HttpContext.Current.Request.Cookies[CookieName];
_cookie.Expires = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Remove(CookieName);
}
}

#endregion
#region Private Methods
/// <summary>
/// this function checks whether the Cookie Existed with the specified CookieName
/// </summary>
/// <returns> </returns>
private bool CookieExisted()
{
return ((HttpContext.Current.Request.Cookies[CookieName] != null) && (!string.IsNullOrEmpty(CookieName)));
}

private void CreateCookie()
{
_cookie = new HttpCookie(CookieName);
HttpContext.Current.Response.Cookies.Add(_cookie);
if (string.IsNullOrEmpty(CookieSubkey))
_cookie.Value = _cookieValue;
else
AddSubkey(CookieSubkey, _cookieValue);
_cookie.Expires = ExpireDate;
if (Core.IsTestMode)
{
HttpContext.Current.Response.Write("CookieName=" + CookieName + " <Br>");
HttpContext.Current.Response.Write("CookieSubkey=" + CookieSubkey + " <Br>");
HttpContext.Current.Response.Write("CookieValue=" + CookieValue + " <Br>");
HttpContext.Current.Response.Write("CookieExisted=" + CookieExisted() + " <Br>");
}
}
#endregion
}



查看完整回答
反对 2019-06-29
?
冉冉说

将Session保存在State Server里:

1.启动服务“ASP.NET State Service”,
2.然后,修改web.config:
<sessionState
mode= "StateServer "
stateConnectionString= "tcpip=127.0.0.1:42424 "
sqlConnectionString= "data source=127.0.0.1;Trusted_Connection=yes "
cookieless= "false "
timeout= "140000 "
/>

注意://mode= "StateServer "这种模式下即使修改页面也不会丢失session!

查看完整回答
反对 2019-06-29

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信