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…

posted on Tuesday, December 27, 2005 11:32 PM by scott

Comments

Wednesday, December 28, 2005 8:08 AM by jd

# re: Low Profile

an excellent article on the profile object. thanks!
Wednesday, December 28, 2005 9:25 AM by optionsScalper

# re: Low Profile

Outstanding.

---O
Wednesday, December 28, 2005 3:01 PM by stacy

# re: Low Profile

Great Info. Thanks!

Do you see this as a substitution for session vars? Does the performance scale nicely, say with 200,000 users?
Wednesday, December 28, 2005 6:39 PM by scott

# re: Low Profile

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.
Monday, January 02, 2006 8:29 PM by Christopher Steen

# Link Listing - January 2, 2006

7 Tips for becoming a better Software
Developer [Via: ]
Adding and Removing items from an Html ...
Wednesday, January 04, 2006 1:49 AM by Arno Nel

# re: Low Profile

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 ?
Wednesday, January 04, 2006 7:14 PM by scott

# re: Low Profile

Hi Arno:

A quick look at the sprocs would say "no". The ApplicationName will have to match for membership and profiles to work together.
Friday, January 06, 2006 12:04 AM by Arno Nel

# re: Low Profile

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
Friday, January 27, 2006 12:22 AM by Jeff Pegg

# re: Low Profile

How do I setup Profiles to use a SQL Server 2005 Express .mdf file?
Thursday, February 02, 2006 6:26 PM by Tim Bajz

# Profile and ProfileCommon objects!

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,.
Friday, February 03, 2006 9:36 AM by scott

# re: Low Profile

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.
Thursday, February 09, 2006 5:39 AM by Gustavo

# re: Low Profile

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?

Thursday, February 09, 2006 6:05 AM by scott

# re: Low Profile

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.
Tuesday, February 14, 2006 8:22 AM by Jeno Laszlo

# re: Low Profile

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?
Tuesday, February 14, 2006 9:11 AM by scott

# re: Low Profile

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.
Wednesday, February 15, 2006 4:02 PM by David

# re: Low Profile

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?
Wednesday, February 15, 2006 5:07 PM by David

# re: Low Profile

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

ProfileBase profile = ProfileBase.Create("name");
Monday, February 20, 2006 8:59 AM by Jeno

# re: Low Profile

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)
Monday, February 20, 2006 10:52 AM by Ian McCullough

# re: Low Profile

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


Tuesday, February 28, 2006 11:53 PM by Qumer

# re: Low Profile

Hi
Excellent! Very informatic and indepth article about ASP.NET PROFILES
Thank U
Wednesday, March 01, 2006 5:35 AM by Neil

# re: Low Profile

Does anyone have an example of this in action?
Sunday, March 05, 2006 10:10 AM by scott

# re: Low Profile

Neil: Which part are you asking about?
Monday, March 06, 2006 11:24 PM by Shiva

# Dynamic property bags

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.
Wednesday, March 08, 2006 9:14 AM by scott

# re: Low Profile

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.
Tuesday, April 18, 2006 11:36 PM by VDeevi

# How to support Nullable types in ASP.Net 2.0 Profile Properties

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:

http://msdn.microsoft.com/asp.net/downloads/providers/default.aspx?pull=/library/en-us/dnaspp/html/asp2prvdr01.asp

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.
Thursday, April 20, 2006 9:50 AM by scott

# re: Low Profile

VDeevi: I don't know of a workaround for that issue, sorry.
Monday, May 29, 2006 11:13 PM by Mango4u

# re: User specific information and database history

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
Tuesday, June 06, 2006 1:53 PM by bobwigs

# re: Low Profile

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
Tuesday, June 06, 2006 5:59 PM by scott

# re: Low Profile

Robert:

Are you sure the history isnt making its way into the Cache or Application objects?
Wednesday, June 07, 2006 5:37 AM by bobwigs

# re: Low Profile

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.
Wednesday, June 07, 2006 6:49 AM by bobwigs

# re: Low Profile

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.
Wednesday, June 21, 2006 5:54 AM by Ryan

# re: Low Profile

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?
Wednesday, June 21, 2006 8:10 PM by scott

# re: Low Profile

Ryan:

You should be able to just define a class with properties for grouping, and use profile inherits="CustomProfile" in web.config.
Friday, July 07, 2006 2:29 AM by Arjen

# re: Low Profile

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
Saturday, July 08, 2006 5:00 PM by scott

# re: Low Profile

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.
Thursday, July 13, 2006 10:46 AM by mergs

# problem with Profiles in Web Application projects

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:

http://webproject.scottgu.com/CSharp/Migration2/Migration2.aspx

(see "Appendix 2: Migrating Code that works with the ASP.NET 2.0 Profile Object")
Sunday, August 13, 2006 8:14 AM by A.N.Zayed

# Profile.IsAnonymous

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
Wednesday, August 23, 2006 7:09 AM by scott

# re: Low Profile

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.
Wednesday, September 06, 2006 12:20 AM by Aziz

# Profiles with Oracle DB

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?

Wednesday, September 06, 2006 7:54 AM by scott

# re: Low Profile

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:
http://weblogs.asp.net/scottgu/archive/2006/04/13/Source-Code-for-the-Built_2D00_in-ASP.NET-2.0-Providers-Now-Available-for-Download.aspx
Thursday, September 14, 2006 7:17 AM by Malcolm

# re: Low Profile

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.
Tuesday, September 19, 2006 10:34 PM by Rajkumar

# Storing Data

Hi,
If I am using MS-Access or XML file then How do i store Profile data in it.
Wednesday, September 27, 2006 7:26 AM by Zhuo

# re: Low Profile

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?
Saturday, October 07, 2006 7:31 PM by scott

# re: Low Profile

@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).
Sunday, October 22, 2006 3:57 PM by Ben

# Autosaving form data

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.
Sunday, January 21, 2007 9:32 PM by Mike Z

# re: Profile Lifespan

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?