How to use ASP. NET profile

Source: Internet
Author: User

ASP. NET has a set of user-related attribute settings, which can be directly used by configuring in webconfig.

Store and use the unique information corresponding to the user
Display personalized Web Applications
The unique identity of a user is used to identify the user upon re-access.
The user-related types provided by ASP. NET profile are strongly typed.

First, generate a database script and run the Visual Studio 2005 command prompt. Enter the command line aspnet_regsql-a p-sqlexportonly filename.

-A: Indicates adding a database.
P: indicates that the profile database is generated.
-Sqlexportonly: indicates that the SQL script is inverted. You can use other parameters to directly create a database. For details, refer to the aspnet_regsql help description.
Filename: the name of the saved file.
Now that the database has been created, there are four tables aspnet_application used to store the application type. aspnet_profile is used to save the user's attribute information, aspnet_schemaversion is used to store version information, and aspnet_user is used to save user information. You can see from these tables that multiple applications can use the same database to represent user information.

Below is how to configure webconfig to support Profile

First configure the connection string

<Connectionstrings>
<Add name = "profiledatabase" connectionstring = "Server = 192.168.1.2; initial catalog = aspnetdb; user id = sa; Password = sa"/>
</Connectionstrings>
Then add the profile node under the system. Web Node

<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>
For more information, see. Automaticsaveenalbed = "true" indicates that the user attributes are automatically saved after data is submitted. Under the properties node, You need to configure your own user attributes. The Group indicates that this is a group, which is equivalent to this attribute and stores other information. Each attribute is followed by an attribute that allows anonymous users to access. This is the default option and does not need to be entered. If the system reports are configured in anonymous authorization mode, you can add them to the system. Web node to solve this problem.

The following describes how to use the profile attribute of ASP. NET without Ajax.

First, let the user log on, then we need the system to record the user and set it to the login status.

Formsauthentication.setauthcookie(this.txt username. Text, false );
The second parameter indicates whether to maintain the session Status across browsers.

Formsauthentication. signout ();
The following is how to read the 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 ";
}
}

First, we can determine whether the age attribute in the profile is 0. In fact, we can determine any attribute. Assign the attributes in the profile to the display attributes one by one. If the attributes on the group node are met, they can be used like the attributes in the object.

Then save the profile

Protected void btnsaveprofile_click (Object sender, eventargs E)
{
This. profile. Name = this.txt name. text;
This. profile. Age = int32.parse(this.txt age. Text );
This. profile. Email = this.txt email. text;
This. profile. Address. City = this.txt city. text;
This. profile. Address. Street = this.txt Street. text;
This. profile. Address. postalcode = this.txt postalcode. text;

This. lblmessage. Text = This. User. Identity. Name + ": Profile saved ";
}

We can see that the above Code does not explicitly Save the profile. When the attribute of a profile changes, it is submitted to the server, and the profile attribute is automatically saved to the database. The same applies to group nodes.

This is a simple profile application in ASP. NET.

Let's take a look at how to use profile in ASP. NET Ajax. First, add a node under the WebServices node under the scripting node under the System. Web. Extensions node.

<Profileservice enabled = "true"
Readaccessproperties = "name, age, email, address. City, address. Street, address. postalcode"
Writeaccessproperties = "name, age, email, address. City, address. Street, address. postalcode"/>
The profile attribute that can be read when readaccessproperties is enabled. Profile attributes that can be written when writeaccessproperties is used

Then we need to add a scriptmanager on the page.

<Asp: scriptmanager id = "scriptmanager1" runat = "server">
<Profileservice loadproperties = "name, age, email, address. City, address. Street, address. postalcode"/>
</ASP: scriptmanager>
Loadproperties indicates the attributes of the preloaded profile.

First, read the attributes of the profile.

Function loadprofiles ()
{
// Debugger;
SYS. Services. profileservice. Load (
Null,
Loadcompleted );
}

The methods of SYS. Services. profileservice. load are described as follows:

SYS. Services. profileservice. Load (
Propertynames, // name of the profile to be loaded. null indicates all
Loadcompletedcallback, // callback function for successful Loading
Failedcallback, // callback function for loading failure
Usercontext // context object that can be specified at will
);

After reading

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 stores the attributes in the profile. You can directly use attributes such as name and age. Loadcompleted has parameters, which are omitted here.

Function loadcompletedcallback (
Number, // Number of profiles loaded this time
Usercontext, // context object randomly specified by the user
Methodname // that is, "SYS. Services. profileservice. Load"
)

Failed read callback function

Function failedcallback (
Error, // error object
Usercontext, // context object randomly specified by the user
Methodname // that is, "SYS. Services. profileservice. Load"
)

Then, save the profile attributes.

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 );
}
You can use a JSON string to set the attributes indicated by the group label such as address, and use the properties. Save method to convert the attributes of the City address.

Properites. Address = {City: "Shanghai ",
Street: "People Square", postalcode: "20002 "};
Properties. Save (...);
Note: even if the attributes automatically saved are set in webconfig, they are not automatically saved in Ajax. You need to call SYS. Services. profileservice. Save to save them. The complete signature of the Save method is as follows:

SYS. Services. profileservice. Save (
Propertynames, // name of the profile to be saved. null indicates all
Savecompletedcallback, // callback function that is successfully saved
Failedcallback, // callback function for loading failure
Usercontext // context object that can be specified at will
);

Complete signature of the successfully saved callback function

Function savecompletedcallback (
Number, // Number of saved profiles
Usercontext, // context object randomly specified by the user
Methodname // that is, "SYS. Services. profileservice. Save"
)

Save the complete signature of the failed callback function

Function failedcallback (
Error, // error object
Usercontext, // context object randomly specified by the user
Methodname // that is, "SYS. Services. profileservice. Save"
)

Other properties of profileservice

Get_timeout ()/set_timeout (time): set or get the timeout value.
Defaultloadcompletedcallback: the default read completion attribute, specifying a function address. The function signature is similar to loadcompletedcallback.
Defaultsavecompletedcallback: saves the completion attribute by default and specifies a function address. The function signature is similar to savecompletedcallback.
Defaultfailedcallback: read or save failure attribute by default. Specify a function address. The function signature is similar to failedcallback.

Related Article

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.