Ngôn ngữ ASP.NET - Đối tượng Profiles

Profiles dùng để làm gì?

ASP.NET Framework cung cấp cho bạn một thay thế cho Session hay cookie để lưu trữ thông tin của người sử dụng đó là đối tượng Profile
Bạn tạo một Profile bằng cách định nghĩa một danh sách các thuộc tính Profile trong ứng dụng ờ file web.config trong thư mục root. ASP.NET Framework tự động biên dịch một lớp chứa đựng các thuộc tính này.
Các thuộc tính thường dùng của Profile

Thuộc tính Ý nghĩa
Nam Chỉ định tên của thuộc tính.
Type Cho phép chỉ định kiểu dữ liệu của thuộc tính.
Defaultvalue Cho phép chỉ định giá trị mặc định của thuộc tính.
ReadOnly Cho phép tạo thuộc tính chỉ đọc.
serializeAs Cho phép bạn chỉ định cách một thuộc tính được tiếp tục tồn tại thành một tĩnh
sentation Giá trị có thể là: Binary, ProviderSpecific, String, và Xml.
allowAnonnyMous Cho phép người sử dụng nặc danh đọc và thiết lập thuộc tính.
Provider Cho phép bạn kết hợp thuộc tính với một Profile Provider riêng biêt
customeProviderData Cho phép truyền dữ liệu cho một Profile Provider.

Ví dụ: sau đây sẽ đưa ra một Profile với ba thuộc tính: firstName, lastName và NumberOfVisits, vào trang Profiles.aspx:

Trang web.config
 

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>

    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    <anonymousIdentification   enabled="true"></anonymousIdentification>
<trust level="Medium"/>  
     <profile>
       
        <properties>
          <add name="firstName"  allowAnonymous="true" />
          <add name="lastName"  allowAnonymous="true"/>
          <add name="NumberOfVisits" type="Int32" defaultValue="0"   allowAnonymous="true"/>
        </properties>
      </profile>      
    </system.web>

</configuration>

Code trang Profiles.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Profiles.aspx.cs" Inherits="Profiles" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
   <form id="form1" runat="server">

    <div>

     FirstName: <asp:Label ID="firstName" runat="server" /><br />

     LastName: <asp:Label ID="lastName" runat="server" /><br />

     Number of Visit: <asp:Label ID="numbervisit" runat="server" /><hr />

     <asp:Label ID="lblfistname" runat="server" Text="FirstName:" />

     <asp:TextBox ID="txtFirstName" runat="server" /><br />

     <asp:Label ID="lbllastName" runat="server" Text="LastName:" />

     <asp:TextBox ID="txtLastName" runat="server" /><br />

     <asp:Button ID="btnUpdate" Text="Update" runat="server" OnClick="btnUpdate_Click" />

    </div>

    </form>
</body>
</html>

Code behind trang Profiles.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Profiles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    void Page_PreRender()
    {
        firstName.Text = Profile.firstName;
        lastName.Text = Profile.lastName;
        Profile.NumberOfVisits++;
        numbervisit.Text = Profile.NumberOfVisits.ToString();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Profile.firstName = txtFirstName.Text;
        Profile.lastName = txtLastName.Text;
    }
}

Thực thi trang web cho kết quả:

Nhập thông tin click vào nút Update cho kết quả: