惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
GbyAI
GbyAI
C
CERT Recently Published Vulnerability Notes
S
Security @ Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Hacker News
The Hacker News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
S
Securelist
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
Latest news
Latest news
T
Tor Project blog
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Spread Privacy
Spread Privacy
K
Kaspersky official blog
T
The Exploit Database - CXSecurity.com
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V2EX - 技术
V2EX - 技术
L
Lohrmann on Cybersecurity
Google Online Security Blog
Google Online Security Blog
Cyberwarzone
Cyberwarzone
Help Net Security
Help Net Security
The Last Watchdog
The Last Watchdog
C
Cybersecurity and Infrastructure Security Agency CISA
Attack and Defense Labs
Attack and Defense Labs
大猫的无限游戏
大猫的无限游戏
Schneier on Security
Schneier on Security
H
Heimdal Security Blog
O
OpenAI News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
AI
AI
H
Hacker News: Front Page
博客园_首页
博客园 - 【当耐特】
L
LINUX DO - 最新话题
MyScale Blog
MyScale Blog
量子位
Vercel News
Vercel News
C
Cisco Blogs
L
LINUX DO - 热门话题
Y
Y Combinator Blog
T
Threatpost
爱范儿
爱范儿
P
Privacy & Cybersecurity Law Blog

博客园 - JasonBie

使用NPOI编辑Excel C# datagridview 快速导出数据到Excel Outlook2016 不能自动配置企业Exchange的解决办法 Linq实现left join左连接 电脑端微信语音像机器人解决办法 解决sql server collation conflict Asp.net APP 重置密码的方式 jQuery dataTables 列不对齐的原因 JavaScript 获得客户端IP Entity Framework Linq 动态组合where条件 查询SQLSERVER执行过的SQL记录 Asp.net Web API 返回Json对象的两种方式 Read Excel file from C# JavaScript测试工具比较: QUnit, Jasmine, and Mocha Asp.net Form验证后造成URL参数重复的问题 MVC删除数据的方法 Cookie Transferring Information Between Pages View State
Session State
JasonBie · 2012-04-12 · via 博客园 - JasonBie

A Session State Example

 public class Furniture 


    public string Name; 
    public string Description; 
    public decimal Cost; 
 
    public Furniture(string name, string description, 
      decimal cost) 
    { 
        Name = name; 
        Description = description; 
        Cost = cost; 
    } 
}

public partial class SessionStateExample : System.Web.UI.Page 

    protected void Page_Load(Object sender, EventArgs e) 
    { 
        if (!this.IsPostBack) 
        { 
            // Create Furniture objects. 
            Furniture piece1 = new Furniture("Econo Sofa"
                                        "Acme Inc."74.99M); 
            Furniture piece2 = new Furniture("Pioneer Table"
 
                                        "Heritage Unit"866.75M); 
            Furniture piece3 = new Furniture("Retro Cabinet"
                                        "Sixties Ltd."300.11M); 
 
            // Add objects to session state. 
            Session["Furniture1"] = piece1; 
            Session["Furniture2"] = piece2; 
            Session["Furniture3"] = piece3; 
 
            // Add rows to list control. 
            lstItems.Items.Add(piece1.Name); 
            lstItems.Items.Add(piece2.Name); 
            lstItems.Items.Add(piece3.Name); 
        } 
 
        // Display some basic information about the session. 
        
// This is useful for testing configuration settings. 
        lblSession.Text = "Session ID: " + Session.SessionID; 
        lblSession.Text += "<br />Number of Objects: "
        lblSession.Text += Session.Count.ToString(); 
        lblSession.Text += "<br />Mode: " + Session.Mode.ToString(); 
        lblSession.Text += "<br />Is Cookieless: "
        lblSession.Text += Session.IsCookieless.ToString(); 
        lblSession.Text += "<br />Is New: "
        lblSession.Text += Session.IsNewSession.ToString(); 
        lblSession.Text += "<br />Timeout (minutes): "
        lblSession.Text += Session.Timeout.ToString(); 
    } 
 
    protected void cmdMoreInfo_Click(Object sender, EventArgs e) 
    { 
        if (lstItems.SelectedIndex == -1
        { 
            lblRecord.Text = "No item selected."
        } 
        else 
        { 
            // Construct the right key name based on the index. 
            string key = "Furniture" + 
                  (lstItems.SelectedIndex + 1).ToString(); 
 
            // Retrieve the Furniture object from session state. 
            Furniture piece = (Furniture)Session[key]; 
 
            // Display the information for this object. 
            lblRecord.Text = "Name: " + piece.Name; 
            lblRecord.Text += "<br />Manufacturer: "
            lblRecord.Text +=  piece.Description; 
            lblRecord.Text += "<br />Cost: " + piece.Cost.ToString("c"); 
        } 
    } 

}  

 Session State Configuration

 The following listing shows the most important op tions that you can set for the <sessionState> 

element. Keep in mind that you won’ t use all of these details at the same time. Some settings apply only to certain session state  modes, as you’ll see shortly. 

 <?xml version="1.0" ?> 

<configuration> 
    <system.web> 
        <!-- Other settings omitted. --> 
 
        <sessionState 
            
cookieless="UseCookies" 
            cookieName
="ASP.NET_SessionId" 
            regenerateExpiredSessionId
="false" 
            timeout
="20" 
            mode
="InProc" 
            stateConnectionString
="tcpip=127.0.0.1:42424" 
            stateNetworkTimeout
="10" 
            sqlConnectionString
="data source=127.0.0.1;Integrated Security=SSPI" 
    sqlCommandTimeout
="30"
            allowCustomSqlDatabase
="false"
            customProvider
=""
            compressionEnabled
="false"
        />
    </system.web>
</configuration>

 Mode

InProc 

InProc is the default mode, and it makes the most sense for small websites. 

Off 

This setting disables session state management for every page in the application. This can provide a slight performance improvement for websit es that are not using session state.  

StateServer 

With this setting, ASP.NET will use a separate Windows service for state management. This service runs on the same web server, but it’s outside the main ASP.NET process, which gives it a basic level of protection if the ASP.NET process needs to be restarte d. The cost is the increased time delay imposed when state information is transferred between two processes. If you frequently access and change state information, this can make for a fairly unwelcome slowdown. 

When using the StateServer setting, you need to specify a value for the stateConnectionString setting. This string identifies the TCP/IP address of the computer that is running the StateServer service and its port number (which is defined by ASP.NET and doesn’t usually n eed to be changed). This allows you to host the StateServer on another computer. If you don’t change this setting, the local server will be used (set as address 127.0.0.1). 

SQLServer 

This setting instructs ASP.NET to use an SQL Server da tabase to store session information, as identified by the sqlConnectionString attribute. This is the most re silient state store but also the slowest by far. To use this method of state management, you’ll need to have a server with SQL  Server installed. 

When setting the sqlConnectionString attribute, you follow the same sort of pattern you use with ADO.NET data access. Generally, you’ll need to specify a data source (the server address) and a user ID and password, unless you’re using SQL integrated security.  

 Here’s a command that creates the session storage database on the current computer, using the default database name ASPState: 

aspnet_regsql.exe -S localhost -E –ssadd

 

<sessionState mode="SQLServer" 
 sqlConnectionString
="data source=127.0.0.1;Integrated Security=SSPI" 
 ... 
/> 

Custom 

When using custom mode, you need to indicate which session state store provider to use by supplying the customProvider attribute.

Compression 

When you set enableCompression to true, session data is compressed before it’s passed out of process. The enableCompression setting only has an effect  when you’re using out-of-process session state storage, because it’s only in this situation that the data is serialized. 

Application State 

Application state allows you to store global objects that can be accessed by any client. Application state is based on the System.Web.HttpApplicationState class,  which is provided in all web pages through the built-in Application object. 

Application state is similar to session state. It supports the same type of objects, retains information on the server, and uses the same dictionary-based syntax. A common example with application state is  a global counter that tracks how many times an operation has been performed by all the web application’s clients. 

For example, you could create a global.asax event ha ndler that tracks how many sessions have been created or how many requests have been received into  the application. Or you can use similar logic in the Page.Load event handler to track how many times a given page has been requested by various clients. Here’s an example of the latter: 

protected void Page_Load(Object sender, EventArgs e) 

    // Retrieve the current counter value. 
    int count = 0
    if (Application["HitCounterForOrderPage"] != null
    { 
        count = (int)Application["HitCounterForOrderPage"]; 
    } 
 
    // Increment the counter. 
    count++; 
 
    // Store the current counter value. 
    Application["HitCounterForOrderPage"] = count; 
    lblCounter.Text = count.ToString(); 

 Application state isn’t often used, because it’s gen erally inefficient. In the previous example, the counter would probably not keep an accurate count, partic ularly in times of heavy traffic. 

 To prevent this problem, you need to use the Lo ck() and Unlock() methods, which explicitly allow only one client to access the Application state collection at a time. 

 To prevent this problem, you need to use the Lo ck() and Unlock() methods, which explicitly allow only 

one client to access the Application state collection at a time. 
protected void Page_Load(Object sender, EventArgs e) 

    // Acquire exclusive access. 
    Application.Lock(); 
 
    int count = 0
    if (Application["HitCounterForOrderPage"] != null
    { 
        count = (int)Application["HitCounterForOrderPage"]; 
    } 
    count++; 
    Application["HitCounterForOrderPage"]=count;
    //Releaseexclusiveaccess.
    Application.Unlock();
    lblCounter.Text=count.ToString();
}