zoukankan      html  css  js  c++  java
  • ASP.Net MVC Session和Cookies的简单使用

    目标:用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
  • 相关阅读:
    jdk动态代理
    mysql-索引方案
    闭包的有点以及出现的内存泄露2016/4/12
    表单2016/4/8
    cursor
    同一个事件绑定不同的函数
    a:link visited hover active
    对于属性操作,加入属性,移除属性
    offset获取位置
    清除浮动6中方法
  • 原文地址:https://www.cnblogs.com/lang2017/p/6876550.html
Copyright © 2011-2022 走看看