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


    目标:用Session和Cookies实现登陆信息保存和展现

    Cookies实现:

    Controller:

    1 1  //把登陆用户名存到cookies中
    2 2 HttpCookie cook = new HttpCookie("cookusername", UserName.ToString());    
    3 3 Response.Cookies.Add(cook);

    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>

    Session实现:

    controller:

    1  //登陆成功把用户名存入session
    2 Session["username"] = UserName.ToString();

    View:

    1 <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
    2                            aria-expanded="false">
    3                             <i class="fa fa-user-o fa-fw"></i>
    4                             @if (Session["username"]!= null)
    5                             {
    6                                 @Session["username"].ToString();
    7                             }                                              
    8                             <span class="caret"></span>
    9                         </a>

    在web.config设置Session过期时间

    <system.web>
    <sessionState mode="InProc" timeout="30"></sessionState>  <!--session过期时间设置-->
    </system.web>
  • 相关阅读:
    python使用阿里云oss-sdk
    flask-文件上传的使用
    flask-migrate的基本使用
    flask-script的基本使用
    flask-sqlalchemy的基本使用
    alembic的常用参数
    sqlchemy查询的其他操作
    orm层面的删除的注意事项
    C#获取数据库中的Instance
    SOA Demo
  • 原文地址:https://www.cnblogs.com/xinyibufang/p/7245818.html
Copyright © 2020-2023  润新知