Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)归海一刀
published in(发表于) 2014/1/30 0:51:47
Asp.Net,Ajax,学习笔记15,如何使用ASP.NET,Profile_[Asp.Net教程]

Asp.Net Ajax 学习笔记15 如何使用ASP.NET Profile_[Asp.Net教程]

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


首先配置连接字符串





然后在system.web节点下添加profile节点





type="System.Web.Profile.SqlProfileProvider"
connectionStringName="ProfileDatabase"
applicationName="ProfileSample"
description="Sample for ASP.NET profile and Profile Service" />












详细说明一下。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节点下添加一个节点


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





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类似







If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759