How to change the Provider of User Profile at runtime

Source: Internet
Author: User

There is a dedicated Profile Provider in asp.net 2.0 to provide the underlying storage service for the User Profile. (If you are not familiar with the User Profile, please refer to the http://www.cnblogs.com/tonyqus/archive/2005/12/18/userprofilev2.html)

Of course, asp.net also allows us to customize the User Profile Provider, which is convenient for multiple servers or multiple storage media. For example, we can store the Profile on the Oracle server and customize an OracleProfileProvider by implementing the System. Web. Profile. ProfileProvider abstract class. However, we usually set defaultProvider in web. config to switch Provider, which means we cannot modify Provider at runtime. If you want to use SqlProfileProvider, you must always use it. However, this cannot meet some special project requirements, such as using different Profile databases based on different users.

So I found a lot of information on the Internet, but there was no way to modify the Profile Provider at runtime, only the articles on how to modify Membership Provider and how to customize Profile Provider are displayed at runtime. To achieve this, Reflector is used to analyze the default storage method. The conclusion is as follows:

Suppose we have such profile settings in web. config:

<Profile defaultProvider = "AspNetSqlProfileProvider">
<Providers>
<Clear/>
<Add name = "AspNetSqlProfileProvider" connectionStringName = "strConnUserDB" type = "System. Web. Profile. SqlProfileProvider" description = "store Profile data"/>
<Add name = "TextFileProfileProvider" type = "CustomProviders. TextFileProfileProvider, CustomProviders" description = "Text file profile provider"/>
</Providers>
<Properties>
<Add name = "test"/>
</Properties>
</Profile>

If we want Profile ["test"] to use TextFileProfileProvider to save data, we can use the following statement:

ProfileCommon. Properties ["test"]. Provider = Profile. Providers ["TextFileProfileProvider"];
Profile ["test"] = "bbb ";

Next we will analyze the principle.

The Profile we usually use is actually an instance of ProfileCommon, Profile. the Providers attribute is provided in the web. for the custom Provider set in the providers section of config, we can use Profile. providers ["ProviderName"] is used to obtain a custom Provider. Note that ProviderName is the name attribute of each provider in the providers section. The Properties attribute is a static attribute and cannot be called through the ProfileCommon instance, so it can only be called using classes.

In the ProfileCommon class, there is a SettingsPropertyCollection used to store all Profile attributes, and each SettingsProperty represents a Profile attribute. For each SettingsProperty, there are three necessary elements: name, value, and provider, this is why we can set provider for a Profile attribute.

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.