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.
Comments
---O
Do you see this as a substitution for session vars? Does the performance scale nicely, say with 200,000 users?
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.
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 ?
A quick look at the sprocs would say "no". The ApplicationName will have to match for membership and profiles to work together.
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
Could this be due to the fact that my solutions have no App_Code directory or am I missing something else?
Cheers,
Ti,.
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.
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?
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.
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?
ProfileBase profile = ProfileBase.Create("name");
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
Excellent! Very informatic and indepth article about ASP.NET PROFILES
Thank U
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.
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.
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.
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
many thanks.
robert[underscore]wiegers{at}bcbst(dot)com
Are you sure the history isnt making its way into the Cache or Application objects?
thanks.
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.
You should be able to just define a class with properties for grouping, and use profile inherits="CustomProfile" in web.config.
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
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.
webproject.scottgu.com/.../Migration2.aspx
(see "Appendix 2: Migrating Code that works with the ASP.NET 2.0 Profile Object")
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
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?
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/...
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.
If I am using MS-Access or XML file then How do i store Profile data in it.
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?
@Zhuo: Mostly write code, but also read some code, too (using Reflector).
Keep going
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!
Cheers,
Graham.
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.
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.
how can we use profile in "Web application" in vs 2010 ?