Low Profile

New article on OdeToCode: Profiles In ASP.NET 2.0

One good reason to inherit from a custom base class is that we can add additional logic to the Profile object. For instance, we can add validation logic to the Age property to ensure the user provides us with a sensible age, and not a value like 443.

Read more…

Print | posted @ Wednesday, December 28, 2005 4:32 AM

Comments on this entry:

Gravatar # re: Low Profile
by jd at 12/28/2005 4:08 PM

an excellent article on the profile object. thanks!
  
Gravatar # re: Low Profile
by optionsScalper at 12/28/2005 5:25 PM

Outstanding.

---O
  
Gravatar # re: Low Profile
by stacy at 12/28/2005 11:01 PM

Great Info. Thanks!

Do you see this as a substitution for session vars? Does the performance scale nicely, say with 200,000 users?
  
Gravatar # re: Low Profile
by scott at 12/29/2005 2:39 AM

Hi Stacy:

I think the Session and Profile still serve two different purposes. Session is for information you want around only during the current visit, while Profile data sticks around between visits. That being said, a lot of data could work in either - for example - a shopping cart.

For perf the answer is it depends. As long as you don't serialize large object graphs into the database it appears to scale well.
  
Gravatar # re: Low Profile
by Arno Nel at 1/4/2006 9:49 AM

Hi there, am i right in assuming that i can have a shared membership , but different profile and role data ?

eg.
User 1 has membership data in application1, is admin in application 1, but in application2 i can still get his membership details and authenticate him, but he has different roles etc

am i making sense ?
ie. can i change "applicationname" or share applications for Roles, Profiles, and Membership, or any combination ?
  
Gravatar # re: Low Profile
by scott at 1/5/2006 3:14 AM

Hi Arno:

A quick look at the sprocs would say "no". The ApplicationName will have to match for membership and profiles to work together.
  
Gravatar # re: Low Profile
by Arno Nel at 1/6/2006 8:04 AM

Just tested and it works.

in web.config.


if you setup applicationname = for membership = / and then do the same for role and prfile, this means usersA will be authenticated off application / use its Roles and membership.

but if you create a new application, but in web.config, you put / for membership, but "app1" for roles, and "app1" for profile, this means that userA will be authenticated against / but will have new roles and profile.

this works great in an intranet environment, where you want 1 db for users to be authenticated, but different applitions to have different roles or profiles.

next stop, having a base profile for / and letting application "app1" have access to the same profile info, but additionally have some of its own. eg. http://intranet has profile info Firstname, Surname, but when i create new application http://intranet/shop, i can use Firstname, Surname, but also store stuff like FaveColor etc

think i read someonewhere on your blog about Profile Inheritnace, will give it a bash
  
Gravatar # re: Low Profile
by Jeff Pegg at 1/27/2006 8:22 AM

How do I setup Profiles to use a SQL Server 2005 Express .mdf file?
  
Gravatar # Profile and ProfileCommon objects!
by Tim Bajz at 2/3/2006 2:26 AM

I am using the Visual Studio 2005 Web Application Project model for my site configuration. I have created a custom sql profile provider, with relevant web.config entries, however I can not reference the Profile nor the ProfileCommon classes in my codebehind files?

Could this be due to the fact that my solutions have no App_Code directory or am I missing something else?

Cheers,
Ti,.
  
Gravatar # re: Low Profile
by scott at 2/3/2006 5:36 PM

Hi Tim:

The compilation model is differetn with WAP. Without WAP the ASP.NET runtime can code-generate a partial class that will have a strongly typed profile object.

With WAP all the compilation is done before ASP.NET can step in, so you can't have a strongly typed Profile property generated.

What I'd do is use the profile inheritance feature in the article to use a profile object defined in my code.
  
Gravatar # re: Low Profile
by Gustavo at 2/9/2006 1:39 PM

scott,

good article...

But I was wondering something about Profile Providers.

As I can see in some articles over the internet, when you implement your own Profile Provider, in the "SetPropertyValues" method hey say to you persist your data in that moment, for example, open a DB connection and update the column in profile table for that property of the profile.

Ok, its works fine... But dont you think its a expensive way to do that?

I mean, for every property changed in the user profiles you have 1 BD connection. Thinks when the user changes all his information in the profile [address, birthdate, etc...].

My question is: Is that right or Is there other way to do that?

  
Gravatar # re: Low Profile
by scott at 2/9/2006 2:05 PM

Gustavo:

I believe saving during SetPropertyValues is the correct approach.

SetPropertyValues isn't called each time you chnage a Profile property. Instead, the HttpModule Profile module will tell the profile to save itself during the OnLeave event (essentially the EndRequest event of an HttpApplication). To persist itself the profile object then ends up calling SetPropertyValues. So, typically SetPropertyValues will only happen once per request.
  
Gravatar # re: Low Profile
by Jeno Laszlo at 2/14/2006 4:22 PM

Profiles are really cool, but I don’t really understand what happens with the SQL Server entries created form anonymous users. As I understand the framework creates a new profile for them and uses a GUID for username and a cookie to identify the client. What happens if the cookie is deleted or expired? If the user visits the site again, he will get a new profile, which is file, but what happens with the old entries? If there is no proper cleanup mechanics, the database will contain a lot of redundant profile info. Is there a SQL job or something to remove the old entries?
  
Gravatar # re: Low Profile
by scott at 2/14/2006 5:11 PM

Jeno: that is correct. If the user deletes the cookie they will get a new GUID and new record the next time. There is no built-in SQL job to clean up old entries, but it wouldn't be too hard to create one based on info in the profile table.
  
Gravatar # re: Low Profile
by David at 2/16/2006 12:02 AM

I like the article, thank-you for writing it.

I have a question about what happens when you use page level output caching though...
HttpContext.Current.Profile will be null if the page is being served from cache, this is fine until you try to use a substitution callback to access the Profile and insert some dynamic personalized content into an otherwise static page. Is there a way to construct the Profile instance in this scenario?
  
Gravatar # re: Low Profile
by David at 2/16/2006 1:07 AM

To answer my own question: Yes, the following line of code will do it.

ProfileBase profile = ProfileBase.Create("name");
  
Gravatar # re: Low Profile
by Jeno at 2/20/2006 4:59 PM

I checked the stored procedures and it seems that the aspnet_Profile_DeleteInactiveProfiles sp is responsible for cleaning up the unused anonymous profiles.

The sp had three parameters: @ApplicationName, @ProfileAuthOptions and @InactiveSinceDate. @ApplicationName is the name of your app, @ProfileAuthOptions is used to specify the profile types, @InactiveSinceDate specify how old profiles should be removed. If @ProfileAuthOptions is 0, the sp deletes the anonymous profiles, if it is 1 it deletes the normal profiles, if it is 2 it deletes both anonymous and normal profiles.

I think the best way to automate the clean up process it to create a scheduled SQL job and run it periodically (depending on the traffic of your website)
  
Gravatar # re: Low Profile
by Ian McCullough at 2/20/2006 6:52 PM

How did you specify the type of Profile.Pets in <properties> section of the web.config? I cant figure out how to specify generic types in the type= attributes. Most problematic is the case where the type is in a dynamically generated assembly, like the App_Code assembly... Sigh.

Ian


  
Gravatar # re: Low Profile
by Qumer at 3/1/2006 7:53 AM

Hi
Excellent! Very informatic and indepth article about ASP.NET PROFILES
Thank U
  
Gravatar # re: Low Profile
by Neil at 3/1/2006 1:35 PM

Does anyone have an example of this in action?
  
Gravatar # re: Low Profile
by scott at 3/5/2006 6:10 PM

Neil: Which part are you asking about?
  
Gravatar # Dynamic property bags
by Shiva at 3/7/2006 7:24 AM

scott,
Your article is real worth reading and its very informative. I have one doubt.
can you tell me whether dynamic property bags can be created using this.
Am having an XML file and i want my programmers to access the values in them by using intellisense.

eg) consider the following xml file
<MyBag>
<name>shiva</shiva>
</MyBag>

i want them to use inside the program as

txtName.Text = MyBag.name;

can u tell me whether this can be achieved?

NOTE: the xml file will be dynamically generated and the nodes can be more or less, i mean not a fixed number

Thanks in advance.
  
Gravatar # re: Low Profile
by scott at 3/8/2006 5:14 PM

Shiva:

It sounds like you might want to use the xsd.exe tool to generate a class from an XML schema. This can be done dynamically in ASP.NET 2.0 by dropping an .xsd file in the App_Code folder.
  
Gravatar # How to support Nullable types in ASP.Net 2.0 Profile Properties
by VDeevi at 4/19/2006 6:36 AM

Hi Scott,

We are trying to develop an ASP.Net 2.0 web application using the latest features provided by MS. We are implementing the Membership, Roles and Profile providers for this new application basing on the information provided from following URL:

msdn.microsoft.com/.../default.aspx

We also checked your blog for Profile provider in the URL:
http://www.odetocode.com/Articles/440.aspx

We now have a clarification about the support for Nullable types in ASP.Net 2.0 Profile properties. We see that Nullable types do not seem to be supported as profile properties in web.config. They are supported as a property type in a class derived from ProfileBase provided base[PropertyName] is not null. Please clarify us about our assumption, if our assumption is wrong please provide us some information how to implement Nullable types in Profile properties.

Thanks & Regards,
VDeevi.
  
Gravatar # re: Low Profile
by scott at 4/20/2006 4:50 PM

VDeevi: I don't know of a workaround for that issue, sorry.
  
Gravatar # re: User specific information and database history
by Mango4u at 5/30/2006 6:13 AM

Well explained..and now more clear to me.
I do however have a question , as a newbie well
I'm actually trying to create a site where users can log in anonymous only and view a page or pages of data or history of data pertaining only to them. What would be the best way to approach this?

Thanks in advance,
Mango4u
  
Gravatar # re: Low Profile
by bobwigs at 6/6/2006 8:53 PM

great article. thanks for a look under the hood. a quick question however...we're getting mysterious behavior with profiles. if User A goes in and saves some stuff (we're tracking search history) it works fine. User B then signs on, and he's getting User A's history. we've run various tests, and it seems as if User A's data is being saved in memory somehow. are we missing something in the web.config or something? any thoughts?
many thanks.
robert[underscore]wiegers{at}bcbst(dot)com
  
Gravatar # re: Low Profile
by scott at 6/7/2006 12:59 AM

Robert:

Are you sure the history isnt making its way into the Cache or Application objects?
  
Gravatar # re: Low Profile
by bobwigs at 6/7/2006 12:37 PM

thanks for the quick response. yes, it does seem like it's either in the cache or application (my best guess right now would be application based on some of our tests). but we're not sure how it's getting there since we cant find anything in the code that's doing that. is this a default behavior that we can change? or do you know why or how it might be doing that?
thanks.
  
Gravatar # re: Low Profile
by bobwigs at 6/7/2006 1:49 PM

hello again.
I think we tracked it down. the public shared array that we were storing the users history in was apparently perisiting across the whole application. I never would have guessed. clearing out the array on the page load before it's loaded from the database seems to have done the trick.
thanks.
  
Gravatar # re: Low Profile
by Ryan at 6/21/2006 12:54 PM

Great article, but what I am finding hard to find is, how can you use the grouping mechanism when the Profile is defined in code, versus in the Web.config? Should you just create an instance of a new class? Or is there a better way?
  
Gravatar # re: Low Profile
by scott at 6/22/2006 3:10 AM

Ryan:

You should be able to just define a class with properties for grouping, and use profile inherits="CustomProfile" in web.config.
  
Gravatar # re: Low Profile
by Arjen at 7/7/2006 9:29 AM

Scott,

Nice article. One question though:

I made an Url Rewrite module (HttpModule) which uses the HttpApplication.BeginRequest event to start the url rewrite. I chose to use Profile to store the selected language, which I need to get the corresponding page from the database (alias and language).

When I try to access HttpContext.Current.Profile from the HttpApplication.BeginRequest event HttpContext.Current.Profile seems to be null (not yet initialized).

Do you know how to access HttpContext.Current.Profile from HttpApplication.BeginRequest?

Thanks,

Arjen
  
Gravatar # re: Low Profile
by scott at 7/9/2006 12:00 AM

Arjen:

I don't believe it will be possible in the BeginRequest event. Profiles are loaded by another HttpModule that catches a later event to load the profile.
  
Gravatar # problem with Profiles in Web Application projects
by mergs at 7/13/2006 5:46 PM

If you are using Web Application project model (as opp to Web Site) you may not see Profile.MyCustomProfile (for example) appear in Intellisense, even when its correctly entered in web.config. If so, the problem and workaround is here:

webproject.scottgu.com/.../Migration2.aspx

(see "Appendix 2: Migrating Code that works with the ASP.NET 2.0 Profile Object")
  
Gravatar # Profile.IsAnonymous
by A.N.Zayed at 8/13/2006 3:14 PM

Hi

I have a problem which I can't solve I want to allow only the register user to access the page in the Page_Load() I wrote

IF Profile.IsAnonymous = true Then

// Code

End If

The problem is the Profile.IsAnonymous = true all the time also with the logged in user

How can I solve this problem

Thanks
  
Gravatar # re: Low Profile
by scott at 8/23/2006 2:09 PM

Hi A.N.Zayed, I'm not sure what could be causing that problem. I've been able to use IsAnonymous to return false for a logged-in user.
  
Gravatar # Profiles with Oracle DB
by Aziz at 9/6/2006 7:20 AM

HI,
I would like to thank you in the first place for your clean article. After reading your article , now i have some idea about profiles.
I would like to know more about using profiles with oracle db.
Do we have to create oracle schemas for storing the data, if yes then can we have the same shema as it is for sql?(for sql server we have a tool)
Do we have to write code in the custom profile provider to acces the database and add entries to it?

  
Gravatar # re: Low Profile
by scott at 9/6/2006 2:54 PM

Hi Aziz:

Yes, you'll need to create your own tables for storing profiles in Oracle. You could copy the SQL Server schema to get a head start, and the source code for the SQL Server Profile provider is available, you might be able to make some straightforward modifications and get it working with Oracle.

See:
weblogs.asp.net/...
  
Gravatar # re: Low Profile
by Malcolm at 9/14/2006 2:17 PM

Great article, I found it very useful.

Do you know if it is possible to stop the profile being created on SQL server for anonymous users if cookies are not supported by the client?

The reason I ask is because my company has the Google Search Appliance to index the content of our intranet site and everytime it connects a new profile is created. This is leading to thousands of profiles being created daily!

I was hoping to be able to either capture the event of the profile being created and stop it, or delete the profile at the end, but I don't really know where to start.
  
Gravatar # Storing Data
by Rajkumar at 9/20/2006 5:34 AM

Hi,
If I am using MS-Access or XML file then How do i store Profile data in it.
  
Gravatar # re: Low Profile
by Zhuo at 9/27/2006 2:26 PM

Fantastic article!

How did you managed to find out all that information? I would like to do some deeper digging into how the .net framework works, but I am not sure what's the best approach to start digging. Any recommendations?
  
Gravatar # re: Low Profile
by scott at 10/8/2006 2:31 AM

@Rajkumar: You'll have to write a custom profile provider. There are whitepapers available on MSDN to do this.

@Zhuo: Mostly write code, but also read some code, too (using Reflector).
  
Gravatar # Autosaving form data
by Ben at 10/22/2006 10:57 PM

A really helpful article! I recently wrote a blog on autosaving form data to the profile (http://thecodeabode.blogspot.com) - your blog helped a lot.
  
Gravatar # re: Profile Lifespan
by Mike Z at 1/22/2007 5:32 AM

Good article thanks. Can anyone explain what is the lifespan of the profiles and cookies? I note that when the Application Pool recycles a new Session Key gets generated for a visitor, meaning they cannot access their existing profile, and a new one has to be built. I would have thought that as everything is getting saved to a database, that the Session Key saved in the cookie would work as long as the cookie does not expire and the database records exist, but that's not happening. Any thoughts?
  
Gravatar # re: Low Profile
by kered at 1/15/2010 5:47 AM

Great article on profiles I am using them in my site and can now extend them due to the information in this article.
  
Gravatar # re: Low Profile
by Ananymous at 1/28/2010 5:10 AM

Excellent:
Keep going
  

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 4 and 8 and type the answer here: