码迷,mamicode.com
首页 > Web开发 > 详细

ASP.Net MVC Session和Cookies的简单使用

时间:2017-05-19 10:10:21      阅读:196      评论:0      收藏:0      [点我收藏+]

标签: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的简单使用

标签:drop   nstat   stat   timeout   one   data   过期   tostring   mod   

原文地址:http://www.cnblogs.com/lang2017/p/6876550.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!