我用asp.net技术做了一个网站,我想把用户输入的用户名保存起来,直到这个网站关闭的时候才将保存的用户名释放掉。我做的是一个客户服务系统的网站,所以用户名的保存时间要长很多,至少要能保存一天左右。不知道cession和cookies那个更容易实现,且消耗的资源少。
    最好能有用户名的存储和读取的例子,麻烦把代码写出来。急!!!谢谢了

解决方案 »

  1.   

    用session和cookies结合,
    登录时给Session值,Server.Session["UserName"]="用户名",取值用Session["UserName"].Value
    同时将用户名和密码保存在cookie上,如果session过期了,从cookie上取,再次给session赋值
      

  2.   

    我是懒人,通常用session,如果说你要访问者关闭浏览器就释放的话,session更合适,如果说要保存一天的话,cookie就更合适。
    session:  session["UserName"]=txtUserName.Text;
    cookie:Response.Cookies["UserName"].value=txtUserName.Text;
      

  3.   


    这样也是麻烦!    不胜直接给cookie
      

  4.   


    保存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
    }
    其实很简单的..  希望楼主能多自己琢磨琢磨.。  能学到手才是自己的.。  加油
      

  5.   

    session是保存在服务器上的
    要想消耗资源少,就用cookie保存在客户端写: 
    HttpCookie   cookie   =new   HttpCookie   ( "user "); 
    cookie.Values.Add   ( "userName ","q107770540"); 
    Response.AppendCookie   (cookie); 
    读: 
    HttpCookie   cookie=   Request.Cookies   [ "user "]; 
    string   user_id=cookie.Values[ "userName "];
      

  6.   

    cookie+Session 如果数据量够大 
    就用cookie
      

  7.   

    麻烦各位能将session和cookies的有效时间的设置以及他们失效的方法告诉我吗,谢谢