Asp. NET login Control extension (personalization)

Source: Internet
Author: User
Tags httpcontext

Since the login control involves membership, you have to mention the user Personalization profile object. Personalization allows specific personalization information to be saved to the database, so it differs from ASP. NET state management in that it is possible to persist this information, it is necessary to emphasize the Web application and website a different, web Application cannot be used directly with the profile object, as in website (http://hi.baidu.com/windlhj/blog/item/8f4c4a13779de02fdc5401b7.html,web Application and website convert each other http://blog.csdn.net/guwenzhong/archive/2009/11/10/4792814.aspx). The personalization settings are very similar to the settings for membership:

1. Configure the personalization provider

The default personalization provider is SqlProfileProvider, which uses ASP.NET.MDF (what is not very familiar, forget the words to see Membership settings) to store personalized information. In fact, you create a good data store according to the membership settings, and you create a personalized data store, because membership and profile use a database (including the use of Aspnet_regsql.exe to replace the database), The user's personal information is stored in the Aspnet_profile table in the database. The personality provider, like membership profile, is also configured in the root Web. config. Because the database connection node has been configured before, it is only available with the personalization provider:

<system.web>

<profile defaultprovider= "Myfirstsqlprofileprovider" >

<providers>

<clear/>

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

Connectionstringname= "newsystemconnectionstring" applicationname= "Profiledemo"/>

</providers>

</profile>

</system.web>

This configures the personalization provider, whose name is Myfirstsqlprofileprovider.

And since we want to store the user's personalization information into the Aspnet_profile table, we have to add properties to it, and these properties are added in the <properties> in the <profile> node with the <add> tag. and must have at least one property name:

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

The profile property means: Name (attribute name, must) type (attribute type, default is String) serializeAs (format used when serializing)

ReadOnly (Read-only property) DefaultValue (default value of the property) AllowAnonymous (Boolean, whether anonymous users are allowed to read and set this property)

Provider (the personalization provider associated with this property) customProviderData (allows the user to pass custom data to the personalization provider)

Using Personalization Properties

By default, only authenticated users can read and write personalization information to the database, for example:

Assign a value directly to the profile to save the user personalization, very similar to the session

protected void Button1_Click (object sender, EventArgs e)

{

In WebApp, it is not possible to use the profile object directly in the same way as in website, but it is not straightforward to introduce using System.Web.Profile, but you can use Profilebase,profilemanager. Profilemigrateeventargs and other classes.

Store personalization Information

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)

{

Get personalized Information

        label1.text = "Current User:" + HttpContext.Current.Profile.UserName + "QQ:" +     & nbsp                          ,         &NB Sp  httpcontext.current.profile.getpropertyvalue ("QQ") + "Age:" +               &NBS P                             httpcontext.current . Profile.getpropertyvalue ("age") + "Tel" +                       &NB Sp                   httpcontext.current.profile.getpropertyvalue ("Tel") + "Address" +                               & nbsp       httpcontext.current.profile.getpropertyvalue ("Address");

}

You can also group personalization attributes for easy management: in Web. config <properties> node Nega <group> Add group name and properties:

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

The type of those attributes can also be a class of their own (the class can be used in App_Code (which is the same as the namespace in WebApp (in the case where the property of the class inside it is changed to compile to be smart)) or in the compiled DLL ( A DLL is a form of an assembly (the other is an EXE) but in Web. config It is important to write the namespace in which the whole class resides. The class name, otherwise there is no type error found:

such as <add name= "Zidingde" type= "dingyibymyself.personlity"/> Property name Zidingde Type write the namespace where the class is located. Class name Dingyibymyself. Personlity

Again like the custom class in App_Code <add name= "ZiDingDe2" type= "WebApplication1.App_Code.personlity"/> Is it a sub-namespace?

Http://www.cnblogs.com/xlb2000/archive/2010/08/25/1796405.html (note the author at the end of the interpretation of serialization and the serialization of the profile to the custom class) in aspnet_ Proile table PropertyNames is the property name (including the custom property name) Propertyvaluesstring is the value of the Simple Type property propertyvaluesbinary is the value of the custom Type property ( The object of the class is already serialized by XML or binary. Because the object of a class is obtained when a custom type property value is obtained (it has been deserialized back to XML or binary), to get the property value in the custom class, you first create a reference to it, and then use the reference to get the property value in the custom class:   protected void Button1_Click (object sender, EventArgs e)         {           &NBSP ;p Erson pp = (person) HttpContext.Current.Profile.GetPropertyValue ("Zidingde");            label1.text = "Current User:" + HttpContext.Current.Profile.UserName +             &NBS P                     "QQ" +pp. qq+ "Age" +pp. Age+ "Tel" +pp. tel+ "Address" +pp. address;         anonymous user personalization by default, anonymous users cannot save personalization information, but sometimes (such as a shopping network anonymous user can also add items to the shopping cart) You need to create some temporary personalization information for anonymous users. Asp. The anonymous identity feature of net can generate a random identifier for anonymous users to uniquely identify the anonymous user and then save the personalization information of the anonymous user to the database. But anonymous.Name personalization saves a lot of useless information in the database. Turn on anonymous personalization method: Add <anonymousidentification enable= "true"/> element under the <system.web> node of the root Web. config. Then specify in <properties> which attributes can be used by anonymous users to hold information (such as <add name= "book" allowanonymous= "true"/>). The anonymous user can then save the personalization attribute information as if it were saved by the user. Profile implementation Anonymous Shopping Cart: http://www.cnblogs.com/jason-xiao/archive/2009/03/18/1415343.html Imagine if it's just that, it doesn't really work and adds some useless information? (or as an example of a shopping network, if an anonymous user adds a few items to the shopping cart when anonymous, and then the user logs in again, the item that he previously added is copied to their user account), so ASP. NET provides the Profile.migrateanonymous event ( This event is triggered as long as the anonymous identification is valid and the current user is authenticated through it) can be in the Global.asax (Global application file: The file contains code that responds to an application-level and session-level event that is raised by an ASP. NET or HTTP module only when you want to handle application events or conversation events should be created), this event is handled in 3 steps: 1. Call the Profile.getprofile method to get the personalization information for an anonymous user 2. Copy the personalization information into the user personalization information after the anonymous user logs in 3. Remove personalization information from anonymity, Clears the anonymous tag. System.web.profile,migrateanonymous event code to reference in Global.asax:  protected void Profile_onmigrateanonymous (object sender, Profilemigrateeventargs e)     {        //webapp still cannot use ProfileCommon class, It seems that this can be used to make use of Web Profile Builder.         //get personalized information for anonymous users     &nbsp      profilecommon anonprofile = Profile.getprofile (e.anonymousid);        // Copy user personalization information from anonymous to personalized information after user login         profile. User Properties =anonprofile. Anonymous Properties         //personalization information and anonymous identifiers when you delete anonymous         profilemanager.deleteprofile (E.ANONYMOUSID);         anonymousidentificationmodule.clearanonymousidentifier ();        //Anonymous users generated when you delete anonymous         membership.deleteuser (e.anonymousid,true);     }  Automatically save personalization information the default ASP. Automatically saves personalization information every time the page ends its life cycle, regardless of whether personalization information changes, which wastes resources. So we're going to set it up to save personalization information when the page ends its lifecycle only when the personalization information is changed: 1. automationsaveenabled= "false" in the <profile> node is not allowed to automatically save personalization information ( That is, we manually call it the Save method) 2. Manually invoke its Save method: After changing the personalization information display call HttpContext.Current.Profile.Save () to manually control, take the example above:           protected void Button1_Click (object sender, EventArgs e)        &NBSP;{&NBS p;      &nbsP   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 ();  //Just add this sentence         }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.