如何使用ASP.NET Profile

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>

 


 

测试成功的配置

 

<profile enabled="true" automaticSaveEnabled="true" defaultProvider="SqlProvider"  >
     
      <providers>
        <add name="SqlProvider"
        type="System.Web.Profile.SqlProfileProvider"
        connectionStringName="ConnectionString"
        applicationName="/"    />
      </providers>
      <!--properties子节点设置用户自定义属性-->
      <properties>
        <!--添加属性格式: name属性名称 type:类型(如System.String)serializeAs:序列化类型-->
        <add name="name"   serializeAs="Xml"/>
        <add name="Type1" serializeAs="Xml"/>
       
      </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类似
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值