会话可以在课堂上存储,以避免会话过期吗? [英] Is sessions can store in class, to avoid session expiration?

查看:82
本文介绍了会话可以在课堂上存储,以避免会话过期吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



在我的asp.net c#web应用程序中,

i创建了一个类来在类中存储会话很长时间时间。

实际上,为了满足会话过期,我将此代码存储在课堂上。



我的问题是:

这是处理会话到期的正确方法/程序吗?



代码:

Hi guys,

In my asp.net c# web application,
i have created one class to store session in class for long time.
Actually, to over come with session expiration, i got this code to store it in the class.

My question is:
Is this the correct way/procedure to handle session expiration or not?

code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
/// <summary>
/// Summary description for MySession
/// </summary>
public class MySession
{
    public MySession()
    {
        //
        // TODO: Add constructor logic here
        //
        Property1 = "default value";
        
    }

    public static MySession Current
    {
        get
        {
            MySession session = (MySession)HttpContext.Current.Session["__MySession__"];
            HttpContext.Current.Session.Timeout = 525600;
            if (session == null)
            {
                session = new MySession();
                HttpContext.Current.Session["__MySession__"] = session;
                HttpContext.Current.Session.Timeout = 525600;
                
            }
            return session;
        }
    }

    

    // **** add your session properties here, e.g like this:
    public string Property1 { get; set; }
    public string AdminFullName { get; set; }
    public string AdminUserName { get; set; }
}





如何分配n获取它,是:



how i assign n fetch it, is:

MySession.Current.AdminFullName = "John";

n

string name = MySession.Current.AdminFullName;





建议。



谢谢



Please suggest.

Thanks

推荐答案

你不能将会话存储在一个类中 - 会话是一个内存,它由IIS存储并维护,直到超时到期并且不再需要它。你的课程寿命更短 - 除非它存储在会话中,否则当页面操作完成并且HTML已经发送给客户端时它将被处理掉。



如果您希望数据在会话期间保持不变,您有两种选择:

1)将其存储在数据库中。这需要登录某种形式以确保两个用户各自获得正确的信息。

2)将其存储在Cookie中 - 这些在客户端系统上保存指定的时间段(可以是几年)并允许您在需要时将其提取回服务器。
You can't "store the session in a class" - the Session is a "memory" which is stored by IIS and maintained until the timeout has expired and it is no longer needed. Your class has an even shorter life - unless it is stored in the Session it will be disposed of when the page operations are complete and the HTML has been sent to the client.

If you want data to be persisted from session to session, you have two options:
1) Store it in a DB. This requires a login of some form to ensure that two users each get the correct information.
2) Store it in a Cookie - these are held on the client system for a specified time period (which can be years) and allow you to fetch it back to the server when it's needed.


这篇关于会话可以在课堂上存储,以避免会话过期吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆