开发者

Multiple profile providers

I have multiple profile providers and profile types:

ProviderA with ProfileA

and

ProviderB with ProfileB

They both use a different database. I want to be able to say:

ProfileB.Create(...), and the profile is cre开发者_运维知识库ated in database B, whilst ProfileA.Create(...) creates the profile in database A.

How the hell do I configure this in my web.config?

The following is (of course) invalid:

    <profile inherits="ProfileA, Authenticatie" defaultProvider="ProfileProviderA" enabled="true" automaticSaveEnabled="true">
        <providers>
            <add name="ProfileProviderA" applicationName="websiteA" type="ProfileProviderA, Authenticatie" connectionStringName="connstringA" description=""/>
        </providers>
    </profile>
    <profile inherits="ProfileB, Authenticatie" defaultProvider="ProfileProviderB" enabled="true" automaticSaveEnabled="true">
        <providers>
            <add name="ProfileProviderB" applicationName="websiteB" type="ProfileProviderB, Authenticatie" connectionStringName="connstringB" description=""/>
        </providers>
    </profile>


I have resolved this with multiple profiles and providers, using the following hack:

First: create a base class for your profiles. It doesn't have to contain all the fields; it's just important they share the same base class (I called this CustomProfileBase).

Furthermore the following change in config:

app.config

<system.web>
    <membership defaultProvider="CustomSqlProviderA" userIsOnlineTimeWindow="15">
        <providers>
            <clear/>
            <add name="CustomSqlProviderA" applicationName="websiteA" type="Authenticatie.A.CustomMembershipProvider, Authenticatie" description="A Membership" connectionStringName="profilesA" />
            <add name="CustomSqlProviderB" applicationName="websiteB" type="Authenticatie.B.CustomMembershipProvider, Authenticatie" description="B Membership" connectionStringName="profilesB" />
        </providers>
    </membership>
    <profile inherits="Authenticatie.CustomProfileBase, Authenticatie" defaultProvider="AProfielen" enabled="true">
        <providers>
            <add name="AProfielen" applicationName="websiteA" type="Authenticatie.A.CustomProfileProvider, Authenticatie" connectionStringName="profielenA" description="A"/>
            <add name="BProfielen" applicationName="websiteB" type="Authenticatie.B.CustomProfileProvider, Authenticatie" connectionStringName="profielenB" description="B"/>
        </providers>
    </profile>
</system.web>

code

// find the membershipprovider based on the property 'website'
var membershipProvider = Membership.Providers.Cast<MembershipProvider>().Single(s => s.ApplicationName == (website == Website.A ? "websiteA" : "websiteB"));
// find the according profileProvider
var profileProvider = ProfileManager.Providers[website == Website.A ? "AProfielen" : "BProfielen"];

// here's the hacky part. There is a static field on the ProfileManager
// that needs to be set. 'Membership' uses the ProfileManager to retrieve
// and store the profile; so it's pretty much the only way
FieldInfo cPr = typeof(ProfileManager).GetField("s_Provider", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
cPr.SetValue(null, profileProvider);

// Now we can retrieve the current user through our selected membershipProvider
var user = membershipProvider.GetUser(gebruikersData.EmailAdres, false);

if (user == null)
{
    // create user:
    membershipProvider.CreateUser(mail, password, mail, null, null, true, null, out createStatus);

    // create according profile. ProfileBase uses Membership internal.
    var profile = (CustomProfileBase)ProfileBase.Create(mail);

    // set the default values, you can upcast your profile again

    profile.Save();
}

Now we have created a user in the according database along with a profile. You can retrieve the user through the membershipProvider and the profile through ProfileManager.FindProfilesByUserName().


You can't have more than 1 profile in web.config but a profile can have multiple providers so maybe you should start thinking about changing your architecture to only have 1 profile, maybe even mixing them together. Then you'd have this kind of web.config:

<profile inherits="ProfileA" defaultProvider="ProfileProviderA" enabled="true" automaticSaveEnabled="true">
    <providers>
        <add name="ProfileProviderA" applicationName="websiteA" type="ProfileProviderA, Authenticatie" connectionStringName="connstringA" description=""/>
        <add name="ProfileProviderB" applicationName="websiteB" type="ProfileProviderB, Authenticatie" connectionStringName="connstringB" description=""/>
    </providers>
    <properties>
        <clear/>
        <add name="property1" type="type1" provider="ProfileProviderA" />
        <add name="property2" type="type2" provider="ProfileProviderB" />
    </properties>
</profile>

The other solution would be to divide your site in 2 subdirectories/subapplications based on your profiles and in each subapplication create it's own web.config with desired profile.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜