ASP.NET登录控件延伸(个性化)

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

既然由登录控件涉及到了Membership,那么就不得不提到用户个性化PRofile对象。个性化允许为用户保存特定的个性化信息到数据库中,因此它不同于asp.net状态管理之处在于可以永久性保存这些信息,很有必要强调一下webapplication与website的一个不同,web application中无法象website中那样,直接用Profile对象(http://hi.baidu.com/windlhj/blog/item/8f4c4a13779de02fdc5401b7.html,web application和website相互转化http://blog.csdn.net/guwenzhong/archive/2009/11/10/4792814.aspx)。个性化的设置与Membership的设置非常类似:

1.配置个性化提供者

默认个性化提供者是SqlProfileProvider,它使用ASP.NET.MDF(怎么样是不是非常熟悉,忘记的话去看Membership的设置)存储个性化信息。实际上你按照Membership的设置创建好数据存储,也就把个性化的数据存储创建好了,因为Membership与Profile用的是一个数据库(包括利用aspnet_regsql.exe更换数据库以后),用户的个性信息保存在数据库的aspnet_profile表中。与Membership一样Profile的个性提供者也是在根web.config里配置的。因为之前已经配置好了数据库连接节点,所以这里只配个性化提供者就行了:

<system.web>

<profile defaultProvider="MyFirstSqlProfileProvider">

<providers>

<clear/>

<add name="MyFirstSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider"

connectionStringName="newsystemConnectionString" applicationName="ProfileDemo"/>

</providers>

</profile>

</system.web>

这样就配置好了个性化提供者,其名字是MyFirstSqlProfileProvider。

而我们既然要存储用户的个性化信息到aspnet_profile表中,我们必须为其添加属性,而这些属性要在<profile>节点内的<properties>里用<add>标签添加,并且至少要有一个属性名:

<system.web>

<profile defaultProvider="MyFirstSqlProfileProvider">

<providers>

<clear/>

<add name="MyFirstSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider"

connectionStringName="newsystemConnectionString" applicationName="ProfileDemo"/>

</providers>

<properties>

<add name="QQ" type="string"/>

<add name="Age" type="Int32"/>

<add name="Address" type="string"/>

<add name="Tel" type="string"/>

</properties>

</profile>

</system.web>

profile属性的意思:name(属性名,必须)type(属性类型,默认是string)serializeAs(当序列化时使用的格式)

readOnly(只读属性)defaultValue(属性的默认值)allowAnonymous(布尔值,是否允许匿名用户读取和设置该属性)

Provider(该属性关联的个性化提供者)customProviderData(允许用户传递自定义数据到个性化提供者)

使用个性化属性

在默认情况下只有验证用户才能读取和写入个性化信息到数据库,举例如下:

//直接给profile赋值就可保存用户个性化,很类似session

protected void Button1_Click(object sender, EventArgs e)

{

//在webapp中无法像website中那样直接使用profile对象,引入using System.Web.Profile还是无法直接用,但可以使用ProfileBase,ProfileManager,ProfileMigrateEventArgs等类。

//存储个性化信息

HttpContext.Current.Profile.SetPropertyValue("QQ", TextBox9.Text);

HttpContext.Current.Profile.SetPropertyValue("Age", int.Parse(DropDownList2.SelectedValue));

HttpContext.Current.Profile.SetPropertyValue("Tel", TextBox10.Text);

HttpContext.Current.Profile.SetPropertyValue("Address",TextBox11.Text);

}

protected void Button2_Click(object sender, EventArgs e)

{

//获取个性化信息

Label1.Text = "当前用户:" + HttpContext.Current.Profile.UserName + "QQ:" + HttpContext.Current.Profile.GetPropertyValue("QQ") + "Age:" + HttpContext.Current.Profile.GetPropertyValue("Age") + "Tel" + HttpContext.Current.Profile.GetPropertyValue("Tel") + "Address" + HttpContext.Current.Profile.GetPropertyValue("Address");

}

还可以给个性化属性分组便于管理:在web.config中<properties>节点内加<group>再添加组名跟属性即可:

<system.web>

<profile defaultProvider="MyFirstSqlProfileProvider">

<providers>

<clear/>

<add name="MyFirstSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider"

connectionStringName="newsystemConnectionString" applicationName="ProfileDemo"/>

</providers>

<properties>

<add name="QQ" type="string"/>

<add name="Age" type="Int32"/>

<add name="Address" type="string"/>

<add name="Tel" type="string"/>

<group name="Habits">

<add name="likebooks"/>

<add name="likesports"/>

</group>

</properties>

</profile>

</system.web>

那些属性的type类型也可以是自己写的类(该类既可以在App_Code里(App_Code相当于当前命名空间里的子命名空间(在webapp里使用注意其里面类的属性要改成编译才能被智能提示))也可以在已编译好的DLL里(DLL是程序集的一种形式(另一种是exe)但在Web.config里一定要写全类所在的命名空间.类名,否则会出现找不到类型的错误):

如 <add name="ZiDingDe" type="Dingyibymyself.personlity"/> 属性名ZiDingDe 类型 写全类所在的命名空间.类名 Dingyibymyself.personlity

再如在App_Code里的自定义类 <add name="ZiDingDe2" type="WebApplication1.App_Code.personlity"/> 是不是就是子命名空间?

http://www.cnblogs.com/xlb2000/archive/2010/08/25/1796405.html(注意作者在最后对序列化及Profile对自定义类的序列化处理的解释)

在aspnet_proile表里PropertyNames里是属性名(包括自定义的属性名)PropertyValuesString里是简单类型属性的值PropertyValuesBinary里是自定义类型属性的值(此时类的对象已被xml序列化或二进制序列化)。

由于获取自定义类型属性值时获得的是一个类的对象(已经反xml或二进制序列化回来),所以要取得自定义类里的属性值要先创建一个引用指向它,然后再用引用取得自定义类里的属性值:

protected void Button1_Click(object sender, EventArgs e)

{

person pp = (person)HttpContext.Current.Profile.GetPropertyValue("ZiDingDe");

Label1.Text = "当前用户:" + HttpContext.Current.Profile.UserName + "QQ"+pp.QQ+"Age"+pp.Age+"Tel"+pp.Tel+"Address"+pp.Address;

}

匿名用户个性化

默认情况下,匿名用户不能保存个性化信息,但有些时候(比如购物网匿名用户也可以向购物车添加商品)就需要为匿名用户创建一些临时的个性化信息。ASP.NET的匿名识别特性可以为匿名用户产生一个随机的标识符,用来唯一识别该匿名用户,然后将匿名用户的个性化信息保存到数据库。但匿名个性化会在数据库中保存很多无用的信息。开启匿名个性化方法:在根web.config的<system.web>节点下的添加<anonymousIdentification enable="true"/>元素。然后在<properties>中指定哪些属性可以被匿名用户用来保存信息(如<add name="Book"allowAnonymous="true"/>)。然后但匿名用户保存这些个性化属性信息时就像用户保存那样处理就可以了。

Profile实现匿名购物车:http://www.cnblogs.com/jason-xiao/archive/2009/03/18/1415343.html

试想如果仅仅是这样那不就没什么实际作用还白白增加了一些无用信息?(还是举购物网的例子,如果匿名用户在匿名时向购物车添加了一些商品,之后该用户又登录了,那么他之前添加的商品会被复制到其用户帐号下)所以ASP.NET提供了

Profile.MigrateAnonymous事件(只要匿名识别有效且当前用户被验证通过就会触发该事件)可以在Global.asax(全局应用程序文件:该文件包含响应ASP.NET或HTTP 模块所引发的应用程序级别和会话级别事件的代码,只在希望处理应用程序事件或会话事件时,才应创建它)中处理,这这个事件中主要处理3个步骤:1.调用Profile.GetProfile方法获取匿名用户的个性化信息2.将这些个性化信息复制到匿名用户登录后的用户个性化信息里3.移除匿名时的个性化信息,清除匿名标记。

Global.asax中要引用System.Web.Profile,MigrateAnonymous事件代码:

protected void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs e)

{

//webapp依旧不能用ProfileCommon类,看来这能用利用Web Profile Builder了。

//获取匿名用户的个性化信息

ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID);

//将匿名时用户个性化信息复制到用户登录后的个性化信息里

Profile.用户属性=anonProfile.匿名时属性

//删除匿名时的个性化信息和匿名识别符

ProfileManager.DeleteProfile(e.AnonymousID);

AnonymousIdentificationModule.ClearAnonymousIdentifier();

//删除匿名时产生的匿名用户

Membership.DeleteUser(e.AnonymousID,true);

}

自动保存个性化信息

默认ASP.NET每当页面结束生命周期时都会自动保存个性化信息而不管个性化信息是否改变,这会浪费资源。所以我们要设成在只有在改变个性化信息时才在页面结束生命周期时保存个性化信息:

1.将<profile>节点里的automationSaveEnabled="false"就是不让它自动保存个性化信息(也就是我们手动来调用它的保存方法)

2.手动来调用它的保存方法:在改变了个性化信息后显示调用HttpContext.Current.Profile.Save()来手动控制,拿上边的例子来说:

protected void Button1_Click(object sender, EventArgs e)

{

HttpContext.Current.Profile.SetPropertyValue("QQ", TextBox9.Text);

HttpContext.Current.Profile.SetPropertyValue("Age", int.Parse(DropDownList2.SelectedValue));

HttpContext.Current.Profile.SetPropertyValue("Tel", TextBox10.Text);

HttpContext.Current.Profile.SetPropertyValue("Address",TextBox11.Text);

HttpContext.Current.Profile.Save(); //只是多加这一句

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航