OdeToCode IC Logo

Low Profile

Wednesday, December 28, 2005

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…

jd Wednesday, December 28, 2005
an excellent article on the profile object. thanks!
optionsScalper Wednesday, December 28, 2005

stacy Wednesday, December 28, 2005
Great Info. Thanks!

Do you see this as a substitution for session vars? Does the performance scale nicely, say with 200,000 users?
scott Thursday, December 29, 2005
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.
Arno Nel Wednesday, January 4, 2006
Hi there, am i right in assuming that i can have a shared membership , but different profile and role data ?

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 ?
scott Thursday, January 5, 2006
Hi Arno:

A quick look at the sprocs would say "no". The ApplicationName will have to match for membership and profiles to work together.
Arno Nel Friday, January 6, 2006
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
Jeff Pegg Friday, January 27, 2006
How do I setup Profiles to use a SQL Server 2005 Express .mdf file?
Tim Bajz Friday, February 3, 2006
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?

scott Friday, February 3, 2006
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.
Gustavo Thursday, February 9, 2006

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?

scott Thursday, February 9, 2006

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.
Jeno Laszlo Tuesday, February 14, 2006
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?
scott Tuesday, February 14, 2006
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.
David Thursday, February 16, 2006
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?
David Thursday, February 16, 2006
To answer my own question: Yes, the following line of code will do it.

ProfileBase profile = ProfileBase.Create("name");
Jeno Monday, February 20, 2006
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)
Ian McCullough Monday, February 20, 2006
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.


Qumer Wednesday, March 1, 2006
Excellent! Very informatic and indepth article about ASP.NET PROFILES
Thank U
Neil Wednesday, March 1, 2006
Does anyone have an example of this in action?
scott Sunday, March 5, 2006
Neil: Which part are you asking about?
Shiva Tuesday, March 7, 2006
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

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.
scott Wednesday, March 8, 2006

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.
VDeevi Wednesday, April 19, 2006
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:


We also checked your blog for Profile provider in the URL:

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,
scott Thursday, April 20, 2006
VDeevi: I don't know of a workaround for that issue, sorry.
Mango4u Tuesday, May 30, 2006
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,
bobwigs Tuesday, June 6, 2006
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.
scott Wednesday, June 7, 2006

Are you sure the history isnt making its way into the Cache or Application objects?
bobwigs Wednesday, June 7, 2006
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?
bobwigs Wednesday, June 7, 2006
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.
Ryan Wednesday, June 21, 2006
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?
scott Thursday, June 22, 2006

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

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?


scott Sunday, July 9, 2006

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


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

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

scott Wednesday, August 23, 2006
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.
Aziz Wednesday, September 6, 2006
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?

scott Wednesday, September 6, 2006
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.

Malcolm Thursday, September 14, 2006
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.
Rajkumar Wednesday, September 20, 2006
If I am using MS-Access or XML file then How do i store Profile data in it.
Zhuo Wednesday, September 27, 2006
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?
scott Sunday, October 8, 2006
@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).
Ben Sunday, October 22, 2006
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.
Mike Z Monday, January 22, 2007
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 kered Friday, January 15, 2010
Great article on profiles I am using them in my site and can now extend them due to the information in this article.
Gravatar Ananymous Thursday, January 28, 2010
Keep going
Gravatar Graham Monday, August 2, 2010
Hi Scott,

I was wondering if there was a way to enumerate through the Profile Properties as if you didn't know what they were. That way, you could add new properties to the web.config file without ever needing to recompile your profile viewing and setting page.

I have searched through google and hit lots of things but nothing frankly that makes sense to me.

Hope you can help, Great article by the way!

Gravatar Jignesh Thursday, September 23, 2010
Scott, Thanks for this great article about profile and profile providers.

Now a question: In my case, I have multiple profile providers listed in web.config file, and I would like to switch between them as per role of the logged in user. Is this possible? If not, what is the purpose of being able to declare multiple profile providers in web.config file?

If possible, how to do it, or can you point me to an article or documentation about it? If I designate different properties under <properties> section to specific profile providers, can I seem them in code behind file using intellisense?

Thank you much in advance.
Gravatar scott Friday, September 24, 2010
Hi Jignesh:

It's been a long time since I've looked at profile providers, and I'm afraid I don't know how to achieve what you are looking for.
Gravatar sadegh Tuesday, October 19, 2010
how can we use profile in "Web application" in vs 2010 ?
ravi Friday, March 4, 2011
good example.
Comments are closed.