個性化用戶配置
一、簡介
為用戶提供自定義的外觀、內容、布局,當用戶再次訪問的時候,用戶還能看到自己原來的設定。
二、個性化的三大步驟
1. 識別用戶身份
要建立驗證用戶身份的機制
創建識別用戶需求的機制
創建管理用戶的機制
2. 提供個性化服務
針對注冊和匿名用戶提供不同的服務
3. 存貯用戶信息
可以保存用戶的相關信息,以方便下次使用,包括用戶的登陸信息
三、實現個性化服務的三大功能
1. 個性化用戶配置
2. web部件
3. 成員和角色管理
四、為匿名用戶進行個性化設置
web.config配置
<anonymousidentification enabled="true"/>
<profile>
<properties>
<add name="name" allowanonymous="true" />
<add name="lastsubmit" type="system.datetime" allowanonymous="true"/>
<group name="address">
<add name="city" allowanonymous="true"/>
<add name="postalcode" allowanonymous="true"/>
</group>
</properties>
</profile>
代碼:
protected void page_load(object sender, eventargs e)
{
if (!page.ispostback)
{
//顯示用戶配置信息
displayprofileinfo();
}
}
protected void btnsubmit_click(object sender, eventargs e)
{
//保存用戶配置信息到profile屬性中
profile.name = txtname.text;
profile.address.city = txtcity.text;
profile.address.postalcode = txtpostalcode.text;
profile.lastsubmit = datetime.now;
//顯示用戶配置信息
displayprofileinfo();
}
private void displayprofileinfo()
{
//從profile屬性中獲取數據并賦值給服務器控件
txtname.text = profile.name;
txtcity.text = profile.address.city;
txtpostalcode.text = profile.address.postalcode;
datetime time = profile.lastsubmit;
//如果未獲取值則顯示空,否則顯示獲取的值
if (time.year == 1)
{
lablastsubmit.text = "空";
}
else
{
lablastsubmit.text = time.tostring();
}
}
五、為注冊用戶實現個性化用戶配置
web.config配置
<connectionstrings>
<add name="northwindconnectionstring" connectionstring="data source=localhost;initial catalog=northwind;integrated security=true"
providername="system.data.sqlclient" />
</connectionstrings>
<system.web>
<profile>
<properties>
<add name="shoppingcart" type="shoppingcart" serializeas="binary"/>
</properties>
</profile>
<authorization>
<deny users="?"/>
</authorization>
<authentication mode="forms">
<forms loginurl ="login.aspx"></forms>
</authentication>
代碼示例:code 13-2
六、匿名用戶轉化為注冊用戶的處理
global.asax中的設置
void profile_migrateanonymous(object sender, profilemigrateeventargs pe)
{
//獲取匿名用戶的profile對象
profilecommon anonprofile = profile.getprofile(pe.anonymousid);
//如果總價為不為0(說明匿名用戶進行了選擇),則將匿名用戶的profile存儲起來
if (anonprofile.shoppingcart.total != 0)
{
profile.shoppingcart = anonprofile.shoppingcart;
}
//刪除匿名用戶的用戶數據(從aspnet_users表)
membership.deleteuser(pe.anonymousid);
//刪除匿名用戶的profle數據(從aspnet_profile表)
profilemanager.deleteprofile(pe.anonymousid);
//刪除匿名用戶標識
anonymousidentificationmodule.clearanonymousidentifier();
}
示例代碼:code 13-3
七、刪除個性化信息
刪除匿名用戶的個性化信息
profilemanager.deleteprofile(context.request.anonymousid)
刪除注冊用戶的個性化信息
profilemanager.deleteprofile(user.identity.name)
新聞熱點
疑難解答
圖片精選