How to use profile for anonymous users

Source: Internet
Author: User

In the previous blog, I mainly introduced the anonymous tracking mechanism starting with ASP. NET 2.0,

However, there is no specific use of profile for anonymous users,

So this blog post will introduce how to use profile anonymously,

The personal information of anonymous users is saved in the aspnet_users User table,

Note that a user is not necessarily qualified as a member. Only registered users are eligible as members,

Anonymous users are not eligible for membership. Therefore, for anonymous users,

The aspnet_membership data table is not involved,

The profile information is stored in the aspnet_profile data table,

Actually, after understanding the content of the previous blog, it is quite simple to read this blog,

You can directly consider anonymous users as common users (of course, there are details ),

Therefore, operations are similar, but note that,

First, you must enable the anonymous mechanism (as described in the previous blog)

Then, if you want to define an attribute in Web. config for anonymous users to use,

You must setAllowanonymous = "true"

This indicates that this attribute is anonymous,

Now let's look at the demo. What about the demo below? The content is relatively simple,

But it is actually true.

First, my settings for Web. config are as follows:

The anonymous tracking mechanism is enabled and an attribute field-photo

Let's take a look at. aspx.

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

<SCRIPT runat = "server">
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
// Bind all. jpg files in the photo directory to the dropdownlist
Ddlimagename. Items. Clear ();
System. Io. directoryinfo dirinfo =
New system. Io. directoryinfo (server. mappath ("~ /Photo "));
Foreach (system. Io. fileinfo file in dirinfo. getfiles ())
{
If (file. extension. tolower () = ". BMP ")
{
Ddlimagename. Items. Add (
New listitem (file. Name, file. Name ));
}
}
Imguser. Visible = false;
// If this anonymous user has logged in before
// (The database aspnet_user contains records of this anonymous user)
// When the anonymous user also has data in aspnet_profile
// Obtain it.
If (! String. isnullorempty ( Profile. Photo ))
{
Imguser. imageurl = Profile. Photo;
Imguser. Visible = true;
// The following statements cannot be executed,
// Because the attribute "Address" is not published to anonymous users in Web. config
// Anonymous users cannot access
// Profile. Address = "loudi, Hunan ";
}
}
}

protected void btnsava_click (Object sender,
eventargs e)
{< br> // Save the profile of this anonymous user
profile. photo = "~ /Photo/"+
ddlimagename. selectedvalue. tostring ();
profile. save ();
lblmsg. TEXT = "congratulations, saved successfully !!! ";
imguser. imageurl = "~ /Photo/"+
ddlimagename. selectedvalue. tostring ();
imguser. Visible = true;
}

Protected void ddlimagename_selectedindexchanged (Object sender,
Eventargs E)
{
Imguser. imageurl = "~ /Photo/"+
Ddlimagename. selectedvalue. tostring ();
Imguser. Visible = true;
}
</SCRIPT>

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div style = "font-family:; font-size: Small">
Select Avatar & nbsp;
<Asp: Dropdownlist Id =" Ddlimagename "Runat =" server"
Width = "150px" autopostback = "true"
Onselectedindexchanged = "ddlimagename_selectedindexchanged">
</ASP: dropdownlist>
<Br/>
<Br/>
<Asp: Label Id =" Lblmsg "Runat =" server "text =" "> </ASP: Label>
<Br/>
<Br/>
<Asp: Button Id =" Btnsava "Runat =" server"
TEXT = "Save profile" onclick = "btnsava_click"/>
<Br/>
<Br/>
<Asp: Image Id =" Imguser "Runat =" server "/>
<Br/>
</Div>
</Form>
</Body>
</Html>

The following is a demo.

Directly run this page and log on to this website as an anonymous user,

And since I have not logged on to this website before,

Therefore, the guid of an anonymous user is automatically stored on your machine,

Then you can use this cookie to identify you,

This guid code is also used to provide you with the profile function,

No Avatar is shown in the above figure. This indicates that your anonymous identity has not been added to the profile,

Next I will add an avatar 1.bmp

Now you can close the browser. Close the browser and check the new cookie.

Here we can see that ASP. NET just now

The GUID code automatically generated by the anonymous user is stored in the cookie,

Check the database again.

From the two images above, we can see that,

Anonymous Users are also stored in the aspnet_users database (as mentioned earlier ),

Then I open aspnet_membership and you will find that there is no anonymous user you just accessed,

Because anonymous users are not qualified members, they will not appear in the aspnet_membership table,

The following table also needs to be viewed as the aspnet_profile table,

We can see from the above that the operation of this anonymous user has been saved in the aspnet_profile table.

Next, open your browser to view the demo page above,

To check whether the anonymous user has been tracked,

You can see that the Avatar saved to the anonymous user who previously visited the website is loaded.

This shows that the anonymous tracking mechanism has played a role,

If you delete the cookie file created on your machine,

Therefore, ASP. NET cannot recognize you, so you can only assign a new guid code to you,

And create a new anonymous user information in the database !!!

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.