標籤:drop nstat stat timeout one data 到期 tostring mod
目標:用Session和Cookies實現登陸資訊儲存和展現
Cookies實現:
Controller:
//把登陸使用者名稱存到cookies中HttpCookie cook = new HttpCookie("cookusername", UserName.ToString()); Response.Cookies.Add(cook);View Code
View:
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> <i class="fa fa-user-o fa-fw"></i> @if (Request.Cookies["cookusername"]!= null) { @Request.Cookies["cookusername"].Value; } <span class="caret"></span> </a>View Code
Session實現:
controller:
//登陸成功把使用者名稱存入session Session["username"] = UserName.ToString();
View Code
View:
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> <i class="fa fa-user-o fa-fw"></i> @if (Session["username"]!= null) { @Session["username"].ToString(); } <span class="caret"></span> </a>View Code
在web.config設定Session到期時間
<system.web><sessionState mode="InProc" timeout="30"></sessionState> <!--session到期時間設定--></system.web>
View Code
ASP.Net MVC Session和Cookies的簡單使用