什么是ASP.NET Profile
- 可以为每个用户(包括匿名用户)储存信息
- 通过在Web.config中的配置即可在应用程序中使用
- 强类型的属性
- 可以定义属性组
- 可以通过自定义ProfileProvider来扩充
如何使用ASP.NET Profile
- 配置ProfileProvider(可以使用内置的SqlProfileProvider)
- 配置Profile的属性
一个使用ASP.NET Profile的示例
首先,运行打开Visual studio 2008命令提示工具(开始——程序——Visual studio 2008——Visual studio Tool)输入指令:aspnet_regsql -A p -sqlexportonly profileset.sql 回车
这样我们就可以在F:\Microsoft Visual Studio\Microsoft Visual Studio 2008\VC下得到一个名为profileset.sql的文件,命令中的“p”就代码ASP.NET 为profile提供的一个数据库的支持
然后我们打开数据库管理器,我电脑里装的Microsoft SQL Server的develop版本,VS2008带的是Express版本,但是在某些功能上限制,比如它限制数据库最大为4G,虽然这在通常情况下已经是够用的了,但是难免我们会遇到一写很大的数据库,比我上回我就遇到一个宝洁公司的一个项目,他的数据库有10G,这种情况下Express版本就有问题了
在数据库管理工具里运行我们刚才得到的SQL文件,我们最好使用数据库的最高权限管理员登陆,以免遇到一些权限上的问题
然后我们会在数据库里发现一个名为aspnetdb的数据库,这个数据库是默认生成的,名字可以是在我们开始使用命令行的时候指定的,
打开这个数据库,我们可以看到四个表(除系统表以外)aspnet_Applications、aspnet_Profile、aspnet_SchemaVersions、aspnet_Users,这些数据表,按照他的名字,我们就可以猜到它的用途啦
然后打开VS中的web.config,在<configuration>节点下添加connectionStrings节点
<connectionStrings> <add name="ProfileDatabase" connectionString="Data Source=XIAOYAOJIAN;Initial Catalog=aspnetdb;Persist Security Info=True;User ID=sa;Password=*******"/> </connectionStrings>
这个就不多做解释了,学过asp.net的人,估计没一个不明白这个的意思吧
然后,在system.web节点下,添加如下内容
<profile enabled="true" defaultProvider="SqlProvider"> <providers> <clear/> <!--type表示我们使用的ProfileProvider的类型,connectionStringName指定我们上面配置的connectionString,applicationName表示应用程序的名字,将会体现的数据库中--> <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>
这里需要注意一点,在每个属性中都有一个allowAnonymous属性,指定是否可为匿名用户使用,默认是false,如果设置为true,则它也可以为一些匿名用户存储一些信息
接下来我们就可以创建也一个页面使用它
前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProfileUsage.aspx.cs" Inherits="Demo08_ProfileUsage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox> <asp:Button ID="btnLogInOut" runat="server" Height="24px" Text="btnLogIn/Out" Width="113px" OnClick="btnLogInOut_Click" /> <hr /> Name:<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br /> Age:<asp:TextBox ID="txtAge" runat="server"></asp:TextBox><br /> Email:<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br /> City:<asp:TextBox ID="txtCity" runat="server"></asp:TextBox><br /> Street:<asp:TextBox ID="txtStreet" runat="server"></asp:TextBox><br /> Postal Code:<asp:TextBox ID="txtPostalCode" runat="server"></asp:TextBox><br /> <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label><br /> <asp:Button ID="btnShowProfile" runat="server" Text="Show" OnClick="btnShowProfile_Click" /> <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" /> </form> </body> </html>
后台代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Security; public partial class Demo08_ProfileUsage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnLogInOut_Click(object sender, EventArgs e) { if (this.User.Identity.IsAuthenticated) { this.lblMessage.Text = this.User.Identity.Name + " logout!"; FormsAuthentication.SignOut();//注销用户 } else { this.lblMessage.Text = this.txtUsername.Text + " login!"; FormsAuthentication.SetAuthCookie(this.txtUsername.Text, false);//使用户登陆 } } protected void btnShowProfile_Click(object sender, EventArgs e) { if (this.Profile.Age == 0)//没有profile,这是个不完整的做法,我们只做一个示例用 { this.txtName.Text = ""; this.txtEmail.Text = ""; this.txtCity.Text = ""; this.txtAge.Text = ""; this.txtPostalCode.Text = ""; this.txtStreet.Text = ""; this.lblMessage.Text = this.User.Identity.Name + " No profile!"; } else { this.txtName.Text = this.Profile.Name; this.txtEmail.Text = this.Profile.Email; this.txtCity.Text = this.Profile.Address.City; this.txtAge.Text = this.Profile.Age.ToString(); //使用属性组中的内容 this.txtPostalCode.Text = this.Profile.Address.PostalCode; this.txtStreet.Text = this.Profile.Address.Street; this.lblMessage.Text = this.User.Identity.Name + " profile loaded!"; } } protected void btnSave_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!"; } }
验证页面,首先在第一个文本框中属于一个用户名(随意的),点击登陆,输入个人信息,点击SAVE,就可以保存关于这个用户的信息,点击Logout,然后点击SHOW,显示NO PROFILE,然后再登陆刚才那个用户,点击SHOW,则显示个人信息
打开数据库aspnetdb中的aspnet_Applications表,可以看到类似如下内容
在这里我们可以找到我们配置的ApplicationName
然后打开aspnet_Users表,会看到类型如下内容
这里能找到上一个表的ApplicationId,UserName,IsAnonymous等一系列信息这就是我们通过profile设置进来的
保存信息的表,则是存放在了aspnet_Profile这个表里,打开我们可以找到几中存储的方式,可以模糊的看到我们存储的信息,因为它已经通过一种它的规则把它进行了序列化,我们不需要也没必要去读懂它
Profile Service
- ASP.NET AJAX 1.0提供的Application Service中的一个
- 另一个是Membership Service
- .NET Framework 3.5中包含其他的Application Service
使用Profile Service
- 出于安全性考虑,ASP.NET AJAX的Profile Service在默认情况下是不可用的
- <profile Service enabled=”true|false" readAccessProperties="prop1,prop2,…" writeAccessProperties="prop1,prop2,…" />
加载Profile Service
Sys.Services.ProfileService.load(propertyNames,//需要加载的Profile名,null表示全部
loadCompletedCallback,//加载成功的回调函数
failedCallback,//加载失败的回调函数
userContext//可以随意设置的上下文对象
);
function loadCompletedCallback(
number,//本次加载的Profile数量
userContext,//用户随意指定的上下文对象
methodName//即Sys.Services.ProfileService.load
){…}
function failedCallback(
error,//错误对象
userContext,//用户随意指定的上下文对象
methodName//即Sys.Services.ProfileService.load
){…}
获取和修改Profile Service
var properties=Sys.Services.ProfileService.properties;
alert(properties.Name);
alert(properties.Address.City);
……
properties.Address={City:"tianjin",Street:"yashen road",PostalCode:"300350"};
properties.Save(…);
保存Profile Service
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
){…}
一个使用Proile Service的示例
创建一个名为ProfileService.aspx的页面
前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProfileService.aspx.cs" Inherits="Demo08_ProfileService" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script language="javascript" type="text/javascript"> function loadProfiles() { Sys.Services.ProfileService.load(null, loadCompleted); } function loadCompleted() { var properties = Sys.Services.ProfileService.properties; if (properties.Age) {//如果有Profile $get("txtName").value = properties.Name; $get("txtAge").value = properties.Age; $get("txtCity").value = properties.Address.City; $get("txtPostalCode").value = properties.Address.PostalCode; $get("message").innerHTML = "Profile loaded!"; } else { $get("txtName").value = ""; $get("txtAge").value = ""; $get("txtCity").value = ""; $get("txtPostalCode").value = ""; $get("message").innerHTML = " No Profile"; } } function saveProfiles() { var properties = Sys.Services.ProfileService.properties; properties.Name = $get("txtName").value; properties.Age = parseInt($get("txtAge").value, 10); properties.Address.City = $get("txtCity").value; properties.Address.PostalCode = $get("txtPostalCode").value; Sys.Services.ProfileService.save(null,saveCompleted); } function saveCompleted() { $get("message").innerHTML = "profile saved"; } </script> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox> <asp:Button ID="btnLogIn" runat="server" Text="Login" OnClick="btnLogin_Click" /> <asp:Button ID="btnLogout" runat="server" Text="Logout" OnClick="btnLogout_Click" /> <hr /> Name: <input type="text" id="txtName" /><br /> Age: <input type="text" id="txtAge" /><br /> Email: <input type="text" id="txtEmail" /><br /> City: <input type="text" id="txtCity" /><br /> Street: <input type="text" id="txtStreet" /><br /> Postal Code: <input type="text" id="txtPostalCode" /><br /> <div id="message" style="color:Red;"></div><br /> <input type="button" value="Show" onclick="loadProfiles()" /> <input type="button" value="Save" onclick="saveProfiles()" /> </form> </body> </html>
后台代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Security; public partial class Demo08_ProfileService : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnLogin_Click(object sender, EventArgs e) { FormsAuthentication.SetAuthCookie(this.txtUserName.Text, false); } protected void btnLogout_Click(object sender, EventArgs e) { FormsAuthentication.SignOut(); } }
然后我们打开web.config进行配置,找到<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"/>
这样,使用方式和之前的一样,但是我们就使用AJAX的方式使用了Profke Service
其他的一些Profile Service属性
- timeout属性:设置超时时间(Sys.Services.ProfileService.get_timeout()/set_timtout();)
- defaultLoadCompletedCallback属性
- defaultSaveCompletedCallback属性
- defaultFailedCallback属性
Profile Service预加载
AJAX技术是很容易被滥用的,因为我们在对网页进行操作的时候,随便的一鼠标划过效果,可能就会产生一个Round-trip,如果设计上有滥用,则会很大程序上增加了服务端的压力,而且如果设计的不好,有时候不旦没有提高用户体验,反而是降低了,还有呢,就是一个安全性的问题,这个以后会写到,所以就要在合适的时候使用预加载,来减少这种Round-trip
Profile Service预加载的方式就是在ScriptManager中做的一些操作
如果我们要使用这个功能,很简单,只需要在<ScriptManager>中添加类似如下代码
<ProfileService LoadProperties="Name, Email, Address.City, Address.Street" />
Profile Service实现方式
Profile Service的功能
- 使用AJAX方式获取Profile
- 使用AJAX方式保存Profile
如果没有这个Profile Service,我们要做这些事情,则需要在客户端调用一个Web Service,事实上Profile Service它的内部实现就是这样的,他是对客户端执行服务器端方法给出的一种特殊实现,在调用定义的ASP.NET AJAX程序集上的ProfileService类,最终工作的为以下的三个方法
- GetAllPropertiesForCurrentUser
- GetPropertiesForCurrentUser
- SetPropertiesForCurrentUser
晚二十点二十七