c#session

一:

网站开发中,为了保存用户信息我们就会用到session。

Session具有以下特点:

1)Session中的数据保存在服务器端;

(2)Session中可以保存任意类型的数据;

(2)Session默认的生命周期是20分钟,可以手动设置更长或更短的时间。

    存入字符串:     Session["userName"] = "aaa";    

    这样取值:     string str = Session["userName"].ToString();
    如是什么某记录的编号:     session["id"] = 1;    

    这样取值:     int id1 = Convert.ToInt32(Session["userName"]);
    可以存很多类型的数据,怎么转就看两例子吧

   二,使用Session验证用户登录

if (txtName.Text == "mr" && txtPassword.Text == "mrsoft")
{
Session["UserName"] = txtName.Text.Trim();
Response.Redirect("a.aspx");
}

三,设置session过期时间

在Web.config中的 <system.web> </system.web>之间添加
<sessionState 
            mode="InProc"
            stateConnectionString="tcpip=127.0.0.1:42424"
            sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
            cookieless="false" 
            timeout="20" 
    />
其中timeout是过期时间.自己调整为合适的就可以了

猜你喜欢

转载自www.cnblogs.com/gbb44/p/10644111.html