Profile description-profilemodule class

Source: Internet
Author: User

Profile is used to create a user profile and manage profile events,

When a user configuration file is enabled, ASP. NET uses profilemodule to create a user configuration file,

And store it in the profile attribute of the current httpcontext.

Public sealed class profilemodule:Ihttpmodule

From the definition of profilemodule, we can see that,

This profilemodule implements the ihttpmodule interface,

It is also a leaf class, that is, it cannot be inherited,

Since it implements the ihttpmodule interface, it can intercept any request,

In addition, you can modify and control these intercepted requests and pass them to ASP. NET for processing,

Note that the profilemodule also exposes three events.

The description in msdn is:

Profilemodule exposes the following events,

You can process these eventsProgramConfigure authentication in:

    • MigrateanonymousEvent,

    • Used to set the configuration file from the anonymous configuration file when an anonymous user logs on

    • Migrate to an authenticated configuration file.

    • PersonalizeEvent to customize how to create a user configuration file.

    • ProfileautosavingEvent,

    • Used inAutomaticsaveenabledWhen the attribute is set to true

    • Controls how to save user configuration files.

In fact, I have already described one of the above three events in detail in the previous blog,

The event used to switch from the anonymous status to the logon status.Profile_migrateanonymous,

Note that the above three events are created in global. asax,

Corresponding

Profile_migrateanonymous ()

Profile_personalize ()

Profile_profileautosaving ()

Next we will introduce them separately.

Profile_personalize () and profile_profileautosaving () Events

Let's take a look at the profile_personalize () event.

For the personalize event,

Occurs when ASP. NET acquires the current State (such as session state) associated with the current request,

The personalize event can be used to specify the custom profile user configuration file,

If the parameter passed to this event at the end of the personalize eventArgs. Profile! = NULL,

The profilemodule class will use thisArgs. Profile

As the profile attribute value of the current httpcontext,

Although I have said so much here, it is estimated that many people do not understand it,

Let me talk about the general content of the demo that will be introduced below,

For example, a website already has thousands of users,

If I set the profile for a single user,

This is not difficult, but you can manage it through roles,

For example, assume that my website has three roles: Admin, superadmin, and user,

If I want to log on to a user that belongs to these three roles separately, I want to use different profiles,

For example, if I want to use different background colors,

Of course, you can add a background color to each user in the aspnet_profile User table,

Then, when a user logs on, the profile. Background color is used to read the stored color,

Modify the page background color. Of course, the above method can be implemented, but it seems too stupid,

In addition, this will increase the database overhead (

The example here is just the simplest background color,

If there is more content, the database will surge)

Another way is to use role for management,

I first classify all users, and then

(A request is sent, which can be captured by profilemodule)

The parameters passed in are used in the profile_personalize event first.Profileeventargs ARGs

To get the username of the current request,

Then, you can obtain the role according to the username,

Then, retrieve the corresponding profile attribute value for the corresponding role in the database,

Assign this attribute to the current profile object.

(That is, modify the current request and submit it to ASP. NET for processing after modification ),

Then, the profilemodule class assigns this attribute to the profile attribute value of the current httpcontext,

In this way, the background color can be read directly through profile on the page,

The following is a demo of this profile_personalize event.

Let's take a look at some preparations,

For the sake of simplicity, I used ASP. NET Website management tools and added three users to the database.

They are superadmin, admin, and users.

And create a profile for the three

Now that you have prepared, you should create the profile_personalize event.

Create in global. asax

<% @ Application codebehind = "Global. asax. cs"

Inherits = "webform. Global" Language = "C #" %>

<SCRIPT runat = "server">

// Here the personalize event defined in global. asax
Void Profile_personalize (Object sender, Profileeventargs ARGs)
{
Profilecommon USERPROFILE;
If (user = NULL)
{
Return;
}
// Here, I only target authenticated users, excluding anonymous users.
USERPROFILE = (Profilecommon) profilebase. Create (user. Identity. Name, true );
String [] userroles = roles. getrolesforuser (user. Identity. Name );
If (userroles. length> 0)
{
If (userroles [0] = "superadmin ")
{
// Obtain the profile of the superadmins user from the profile
// As the profile of the current request
USERPROFILE = USERPROFILE. getprofile ("superadmins ");
}
Else if (userroles [0] = "admin ")
{
USERPROFILE = USERPROFILE. getprofile ("admins ");
}
Else if (userroles [0] = "user ")
{
USERPROFILE. getprofile ("users ");
}
// The following explains why there are superadmins, admins, and users.
// The reason is that my website has three types of roles. Since I want to uniformly manage these three types of roles,
// Then I should save the profiles of these three roles in the database, and the above three users
// It is not actually used as a user, but used as a storage profile.
// Take superadmins for example. Its profile represents all superadmin profiles.
// In this way, you only need to save the profile of a superadmins user, instead
// Save the same profile for each user in the superadmin role
}
If (USERPROFILE = NULL)
{
Args. Profile = NULL;
}
Else
{
// Use the profile you just obtained to overwrite the original profile of the request (modify)
// Pass it to ASP. NET for processing.
Args. Profile = USERPROFILE;
}
}

</SCRIPT>

Here is a page.

<% @ Page Language = "C #" %>

<SCRIPT runat = "server">

Protected void page_load (Object sender, eventargs E)
{
If (! User. Identity. isauthenticated)
{
System. Web. Security. formsauthentication. redirecttologinpage ();
}
// The profile. Username here indicates that it is in the personalize event
// The Modified Profile passed over
Lblusername. Text = Profile. Username;
Lbladdress. Text = Profile. address;
Lblconstellate. Text = profile. constellation;
// The user. Identity. Name is the user name verified by the password.
Lblname. Text = User. Identity. Name;
Lblphone. Text = Profile. Phone. Home Phone + "" +
Profile. Phone. Mobile phone;
}
</SCRIPT>

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Style type = "text/CSS">
. Style1
{
Width: 45%;
Height: 177px;
Border: 1px solid # 8000ff;
}
. Style2
{
Height: 38px;
Font-size: small;
}
</Style>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div style = "font-family:; font-size: Small">
<Table class = "style1">
<Tr>
<TD style = "font-size: Small">
Login Name </TD>
<TD style = "font-size: Small">
<Asp: Label Id =" Lblname "Runat =" server ">
</ASP: Label>
</TD>
</Tr>
<Tr>
<TD style = "font-size: Small">
Username </TD>
<TD style = "font-size: Small">
<Asp: Label Id =" Lblusername "Runat =" server ">
</ASP: Label>
</TD>
</Tr>
<Tr>
<TD style = "font-size: Small">
Address
</TD>
<TD style = "font-size: Small">
<Asp: Label Id =" Lbladdress "Runat =" server ">
</ASP: Label>
</TD>
</Tr>
<Tr>
<TD style = "font-size: Small">
Phone number </TD>
<TD style = "font-size: Small">
<Asp: Label Id =" Lblphone "Runat =" server ">
</ASP: Label>
</TD>
</Tr>
<Tr>
<TD class = "style2">
Constellation </TD>
<TD class = "style2" style = "font-size: Small">
<Asp:Label Id =" Lblconstellate "Runat =" server ">
</ASP: Label>
</TD>
</Tr>
</Table>
</Div>
</Form>
</Body>
</Html>

Actually, this demo'sCodeNot important,

It is important to let everyone know what the personalize event actually did?

Therefore, we need to take a good look at each part of the demo below.

First, I log on with Xiaozhen (Xiaozhen belongs to the superadmin role)


After successful login

The profile of Xiaozhen is shown above,

But the strange thing is that in the database, I only defined superadmins, admins,

Users, but does not define the profile for Xiaozhen,

But why does it appear here?

Here, the personalize event is working,

After Xiaozhen is successfully logged on, the server code is defined in the Code on the page, so a request is sent,

These requests are captured by profilemodule to trigger the personalize event,

Then I determined in the personalize event that the Xiaozhen user belongs to the superadmin role,

As a result, I assigned the profile attribute of the superadmins user in the database to Xiaozhen,

Therefore, the profile belonging to superadmins is assigned to the profile of Xiaozhen,

Let's take a look at the demo under the admin role (Suha belongs to the admin role)

The above explains so much, and then there is a demo, and the demo effect is also attached,

I think now everyone knows what the personalize event is !!!

The above section describes the personalize event,

Next we will introduce another event of profilemodule-profileautosaving.

The first thing to note is that although the profileautosaving event belongs to the profilemodule class,

However, if you want to enable or disable this function, it depends on another class,

WhenProfilemanager. automaticsavaenabled = true(True by default ),

Indicates that the function of automatically saving the user configuration file at the end of the ASP. NET page is enabled,

If the auto save function is enabled,

Profilemodule will be in the process of endrequest event at the end of ASP. NET page

Trigger the profileautosaving event,

There is also the property of automaticsavaenabled.

You can also use the profile section of Web. config for configuration.

The above mainly explains some basic content of profileautosaving,

What is actually done is not explained yet. Let's explain it,

However, this first describes the function of automatically saving user settings files,

This function means that the profile mechanism will automatically check whether the user's profile attribute has been changed,

If it is changed, it is treated as dirty data (dirty data,

Profile must automatically call Sava () at the end of each page request (otherwise, it is called Automatic Storage)

But the problem is:

From msdn

Settingsbase. Save method check

The isdirty attribute value of each settingspropertyvalue in the user configuration file,

To determine whether the attributes of the primitive type, string, or datetime object have been changed.

The Save method cannot explicitly determine whether the custom class has been changed.

You can use the profileautosaving event to determine whether a custom object has been changed,

Then, save the modified object automatically, or cancel Automatic saving when no object is modified.

Above, that is, the profile automatically checks whether the profile attribute is changed,

It must be a primitive type, string, or datetime,

Only such an object can be detected for any changes,

However, custom classes and Complex Color objects cannot be detected,

What do we have to do with this,

The contact is, which cannot be detected,

This profile automatic detection mechanism will automatically convert them into dirty data (that is, they are not changed ),

This makes the problem big. When you use a complex data type, it will be deemed as you have changed,

That is to say, it will automatically save the request at the end of each page,

My God, if my data type is complex and there are more profile attributes,

If you have more requests, this website can still run. If you save them from time to time, the performance will be compromised,

So,

At this time, you need to use the profileautosaving event to manually control when,

When can I manually disable the auto save function,

In this way, we can solve the performance problems mentioned above. For example, I have not changed this complex type,

In this case, the auto save function is disabled, so the performance is guaranteed,

If the complex type does change, I can manually enable the Automatic save function,

Because the profileautosaving event is triggered at the end of each request,

Therefore, it is easy to enable or disable the Automatic save function manually,

With so many examples, I think it should be clear !!!

The following is a demo.

Let's take a look at the profileautosaving event defined in global. asax.

<% @ Page Language = "C #" %>
<SCRIPT runat = "server">

Protected void page_load (Object sender, eventargs E)
{
If (user. Identity. isauthenticated = false)
{
System. Web. Security.
Formsauthentication. redirecttologinpage ();
}
Lblname. Text =User. Identity. Name;
}

protected void btnsava_click (Object sender, eventargs e)
{< br> If (cboxautosaving. checked)
{< br> // enable the Automatic save profile function
context. items ["profileautosaving"] = true;
}< br> else
{< br> // disable the Automatic save profile function
context. items ["profileautosaving"] = false;
}

Profile. Address = txtaddress. text;
Profile. Phone. Mobile phone = txtphone. text;
Profile. constellation = txtconstellate. text;

}
</SCRIPT>

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Style type = "text/CSS">
. Style1
{
Width: 55%;
Border: 1px solid # 8000ff;
Height: 200px;
}
</Style>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div style = "font-family:; font-size: Small">
<Table class = "style1">
<Tr>
<TD>
User name </TD>
<TD>
<Asp: Label Id =" Lblname "Runat =" server ">
</ASP: Label>
</TD>
</Tr>
<Tr>
<TD>
Address </TD>
<TD>
<Asp: Textbox Id =" Txtaddress "Runat =" server ">
</ASP: textbox>
</TD>
</Tr>
<Tr>
<TD>
Phone number </TD>
<TD>
<Asp: Textbox Id ="Txtphone "Runat =" server ">
</ASP: textbox>
</TD>
</Tr>
<Tr>
<TD>
Constellation </TD>
<TD>
<Asp: Textbox Id =" Txtconstellate "Runat =" server ">
</ASP: textbox>
</TD>
</Tr>
<Tr>
<TD>
<Asp: Checkbox Id =" Cboxautosaving "
Runat = "server" text = "whether to automatically save"/>
</TD>
<TD>
<Asp: Button Id =" Btnsava "Runat =" server"
TEXT = "Save profile"
Onclick = "btnsava_click"/>
</TD>
</Tr>
</Table>
</Div>
</Form>
</Body>
</Html>

From the code above, the method profile. Sava () is not used to save the profile.

Let's take a look at the demo.

First, I log on to this page with Xiaozhen

Then, if you do not select Automatic save, click Save profile,

In this case, you can go to the database to verify that no data is added to the aspnet_profile data table,

This is because the auto save function is not enabled yet,

The following shows how to enable the auto save function,

Click Save profile.

Check the data table aspnet_profile again.

I added it. The most important thing is that I did not display the call to the profile. Sava () method,

This is the automatic save function, and the above shows how to disable the Automatic save function,

The entire demo has been completed, but there is still a reminder that,

The data I used above is simple type,

Therefore, the profile mechanism can completely detect that the profile attribute has not changed,

Therefore, it is not necessary to use the profileautosaving event.

However, if complex data types are to be used,

You must use the profileautosaving event to control when to enable automatic saving,

When to disable automatic save, otherwise the performance will be compromised !!!

Since the previous blog and this blog, we have finished introducing three important events of profilemodule,

These events are all useful and important. I think,

These two blog posts are also detailed. I hope you can master these three events carefully !!!

 

2010-2-10

 

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.