Asp.Net中有一套與用戶相關聯的屬性設置,可以通過在WebConfig裏配置來直接使用,他的作用爲
存儲和使用唯一與用戶對應的信息 展現個人化版本的Web應用程序 用戶的唯一身份標識在再次訪問時識別用戶 Asp.Net Profile提供的跟用戶相關的類型都是強類型
首先生成數據庫腳本,使用Visual Studio 2005 命令提示,輸入命令行aspnet_regsql -A p -sqlexportonly filename
-A:表明增加一個數據庫 p:表明生成的是Profile的數據庫 -sqlexportonly:表明是倒出sql腳本,用其他參數可以直接創建數據庫,具體的說明可以參看aspnet_regsql的幫助說明 filename:保存的文件名 現在數據庫創建好了,裏面有4張表aspnet_Application用來存儲應用程序的相關類型,aspnet_Profile用來保存用戶的屬性信息,aspnet_SchemaVersion用來存儲版本信息,aspnet_User用來保存用戶信息。可以從這幾張表可以看到,多個應用程序可以用同一個數據庫來表示用戶信息。
下面是如何來配置WebConfig來支持Profile
首先配置連接字符串
<connectionStrings> <add name="ProfileDatabase" connectionString="Server=192.168.1.2; Initial Catalog=aspnetdb; User ID=sa; Password=sa"/> </connectionStrings> 然後在system.web節點下添加profile節點
<profile enabled="true" automaticSaveEnabled="true" defaultProvider="SqlProvider"> <providers> <clear /> <add name="SqlProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ProfileDatabase" applicationName="ProfileSample" description="Sample for ASP.NET profile and Profile Service" /> </providers> <properties> <add name="Name" type="System.String"/> <add name="Email" type="System.String" /> <add name="Age" type="System.Int32" /> <group name="Address"> <add name="City" type="System.String" /> <add name="Street" type="System.String" /> <add name="PostalCode" type="System.String" /> </group> </properties> </profile> 詳細說明一下。automaticSaveEnalbed="true",表明用戶在提交數據之後,自動保存用戶屬性。properties節點下面就是用戶需要配置的自己的用戶屬性,group表明這是一個組,相當與這個屬性是一個對象,他保存其他的信息。在每個屬性後面都有一個允許匿名用戶訪問的屬性,這個是默認選項,可以不用輸入。有時候配置了系統報需要使用匿名授權模式,那麼在system.web節點下添加就可以解決這個問題。
下面是使用Asp.Net的Profile屬性,不涉及到Ajax。
首先讓用戶登陸,那麼我們需要讓系統記錄這個用戶,並把他設置爲登陸狀態。
FormsAuthentication.SetAuthCookie(this.txtUserName.Text, false); 第二個參數表明是否跨瀏覽器保持會話狀態,退出登陸使用
FormsAuthentication.SignOut(); 下面是讀取Profile
protected void btnShowProfile_Click(object sender, EventArgs e) { if (this.Profile.Age == 0) { txtName.Text = string.Empty; txtAge.Text = string.Empty; txtEmail.Text = string.Empty; txtCity.Text = string.Empty; txtStreet.Text = string.Empty; txtPostalCode.Text = string.Empty;
this.lblMessage.Text = this.User.Identity.Name + ": No Profile"; } else { txtName.Text = this.Profile.Name; txtAge.Text = this.Profile.Age.ToString(); txtEmail.Text = this.Profile.Email; txtCity.Text = this.Profile.Address.City; txtStreet.Text = this.Profile.Address.Street; txtPostalCode.Text = this.Profile.Address.PostalCode;
this.lblMessage.Text = this.User.Identity.Name + ": Profile Loaded"; } }
我們首先判斷Profile裏面的Age屬性是否爲0,其實可以判斷任何一個屬性。然後將Profile裏面的屬性給一一賦值到顯示屬性上。如果碰到了group節點上的屬性,可以象使用對象裏的屬性一樣使用。
然後是保存Profile
protected void btnSaveProfile_Click(object sender, EventArgs e) { this.Profile.Name = this.txtName.Text; this.Profile.Age = Int32.Parse(this.txtAge.Text); this.Profile.Email = this.txtEmail.Text; this.Profile.Address.City = this.txtCity.Text; this.Profile.Address.Street = this.txtStreet.Text; this.Profile.Address.PostalCode = this.txtPostalCode.Text;
this.lblMessage.Text = this.User.Identity.Name + ": Profile Saved"; }
可以看到上面這段代碼我們沒有顯視的保存Profile,當一個Profile的屬性改變時,提交到服務器,Profile的屬性會自動保存到數據庫。碰到group節點的使用方法也一樣。
這就是簡單的Profile的在Asp.Net裏面的應用。
下面我們來看看在Asp.Net Ajax裏面怎麼使用Profile。首先我們在system.web.extensions節點下的scripting節點下的webServices節點下添加一個節點
<profileService enabled="true" readAccessProperties="Name, Age, Email, Address.City, Address.Street, Address.PostalCode" writeAccessProperties="Name, Age, Email, Address.City, Address.Street, Address.PostalCode" /> readAccessProperties時允許讀取的Profile屬性。writeAccessProperties時允許寫入的Profile屬性
然後我們在頁面上需要添加一個ScriptManager
<asp:ScriptManager ID="ScriptManager1" runat="server"> <ProfileService LoadProperties="Name, Age, Email, Address.City, Address.Street, Address.PostalCode" /> </asp:ScriptManager> LoadProperties表明預加載的Profile的屬性。
首先時讀取Profile的屬性
function loadProfiles() { //debugger; Sys.Services.ProfileService.load( null, loadCompleted); }
Sys.Services.ProfileService.load的方法詳細說明如下
Sys.Services.ProfileService.load( propertyNames, //需要加載的Profile名,null表示全取 loadCompletedCallback, //加載成功的回調函數 failedCallback, //加載失敗的回調函數 userContext// 可以隨意指定的上下文對象 );
讀取完成之後
function loadCompleted() { //debugger; var properties = Sys.Services.ProfileService.properties; if(properties.Age) { get("txtName").value = properties.Name; get("txtAge").value = properties.Age; get("txtCity").value = properties.Address.City; get("txtEmail").value = properties.Email; get("txtStreet").value = properties.Address.Street; get("txtPostalCode").value = properties.Address.PostalCode; get("message").innerHTML = "Profile Loaded"; } else { get("txtName").value = ""; get("txtAge").value = ""; get("txtCity").value = ""; get("txtEmail").value = ""; get("txtStreet").value = ""; get("txtPostalCode").value = ""; get("message").innerHTML = "Profile not Loaded"; } } Sys.Service.ProfileService.properties存儲的就是Profile裏面的屬性,可以直接使用Name、Age等屬性。loadCompleted是存在參數的,這裏我們只是省略了。
function loadCompletedCallback( number, // 本次加載的Profile數量 userContext, // 用戶隨意指定的上下文對象 methodName//即"Sys.Services.ProfileService.load" )
讀取失敗的回調函數
function failedCallback( error, // 錯誤對象 userContext, // 用戶隨意指定的上下文對象 methodName//即"Sys.Services.ProfileService.load" )
然後就是保存Profile的屬性
function saveProfiles() { var properties = Sys.Services.ProfileService.properties;
properties.Name = get("txtName").value; properties.Age = parseInt(get("txtAge").value, 10); properties.Email = get("txtEmail").value; properties.Address.City = get("txtCity").value; properties.Address.Street = get("txtStreet").value; properties.Address.PostalCode = get("txtPostalCode").value;
Sys.Services.ProfileService.save(null, saveCompleted); } 碰到象Address這樣的group標籤標明的屬性可以使用JSon字符串來設置,並且使用properties.save方法來轉換城Address的屬性
properites.Address = {City : “Shanghai”, Street: “People Square”, PostalCode: “20002”}; properties.save(…); 注意,就算在webconfig裏設置了自動保存的屬性,在ajax裏面也不會自動保存,需要調用Sys.Services.ProfileService.save方法進行保存。save方法的完整簽名如下
Sys.Services.ProfileService.save( propertyNames, //需要保存的Profile名,null表示全取 saveCompletedCallback, //保存成功的回調函數 failedCallback, //加載失敗的回調函數 userContext// 可以隨意指定的上下文對象 );
保存成功的回調函數的完整簽名
function saveCompletedCallback( number, // 本次保存的Profile數量 userContext, // 用戶隨意指定的上下文對象 methodName//即“Sys.Services.ProfileService.save” )
保存失敗的回調函數的完整簽名
function failedCallback( error, // 錯誤對象 userContext, // 用戶隨意指定的上下文對象 methodName//即"Sys.Services.ProfileService.save" )
ProfileService的其他屬性
get_timeout()/set_timeout(time):設置或得到超時時間 defaultLoadCompletedCallback:默認讀取完成屬性,指定一個函數地址,函數簽名與loadCompletedCallback類似 defaultSaveCompletedCallback:默認保存完成屬性,指定一個函數地址,函數簽名與saveCompletedCallback類似 defaultFailedCallback:默認讀取或保存失敗屬性,指定一個函數地址,函數簽名與failedCallback類似
|