Session-Sử dụng Profiles trong ASP.NET

Session

Bạn có thể chưa thực sự dùng Cookies để lưu trữ Shoping Cart. Một Cookie vừa quá nhỏ và quá đơn giản. Để làm việc ngoài giới hạn của Cookie ASP.NET Framework hỗ trợ một chức năng mới được gọi là Session State

Giống với Cookie Session lưu trữ dữ liệu trong phạm vi riêng với từng người sử dụng. Nhưng không giống với Cookie Session không giới hạn dung lượng, nếu bạn cần bạn có thể lưu trữ hàng Gigabyte dữ liệu, hơn thế nữa Session có thể đưa ra điều đối tượng phức tạp hơn là chuỗi Text. Bạn có thể lưu trữ một vài đối tượng trong Session. Ví dụ bạn có thể lưu trữ một Dataset hay một Shoping cart trong Session.

Sử dụng đối tượng Session

Chương trình ứng dụng chính giao tiếp và làm việc với Session State là lớp HttpSessionState. Đối tượng này được thể hiện bới các thuộc tính Page.Session, Context.Session, UserControl.Session, Webservice.Session và Application.Session. có nghĩa là bạn có thể làm việc với Session bất kỳ đâu trong ứng dụng web.

Lớp HttpSessionState hỗ trợ các thuộc tính sau:

         CookieMode: có cho phép Cookie Session hay không?

Count: cho phép lấy số dữ liệu trong Session State

IsCookieless: Cho phép chỉ rõ có cho phép Cookieless hay không?

IsNewSession—Enables you to determine whether a new user session was created with the current request.

IsReadOnly—Enables you to determine whether the Session state is read-only.

Keys—Enables you to retrieve a list of item names stored in Session state.

Mode—Enables you to determine the current Session state store provider. Possible

values are Custom, InProc, Off, SqlServer, and StateServer.

SessionID—Enables you to retrieve the unique session identifier.

Timeout—Enables you to specify the amount of time in minutes before the web server assumes that the user has left and discards the session. The maximum value is 525,600 (1 year).

 Đối tượng HttpSessionState hỗ trợ các phương thức sau:

Abandon: Cho phép kết thúc Session của một người sử dụng.

Clear: Cho phép xoá toàn bộ dữ liệu trong Session State.

Remove: cho phép bạn xoá từng phần tử trong Session State