多个配置文件提供商

发布于 2024-09-14 12:52:06 字数 1106 浏览 1 评论 0原文

我有多个配置文件提供程序和配置文件类型:

ProviderAProfileA

ProviderBProfileB

它们都使用不同的数据库。我希望能够说:

ProfileB.Create(...),并且配置文件是在数据库 B 中创建的,而 ProfileA.Create(...) 创建的数据库 A 中的配置文件。

我该如何在 web.config 中配置它?

以下内容(当然)是无效的:

    <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 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 created 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

待"谢繁草 2024-09-21 12:52:06

我已经使用以下技巧通过多个配置文件和提供程序解决了这个问题:

首先:为您的配置文件创建一个基类。它不必包含所有字段;重要的是它们共享相同的基类(我称之为 CustomProfileBase)。

此外,配置中进行了以下更改:

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>

代码

// 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();
}

现在,我们已经在相应的数据库中创建了一个用户以及一个配置文件。您可以通过 membershipProvider 检索用户,并通过 ProfileManager.FindProfilesByUserName() 检索个人资料。

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().

惟欲睡 2024-09-21 12:52:06

web.config 中不能有超过 1 个配置文件,但一个配置文件可以有多个提供程序,因此也许您应该开始考虑将架构更改为只有 1 个配置文件,甚至可能将它们混合在一起。然后你就会有这种 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>

另一种解决方案是根据你的配置文件将你的站点分为 2 个子目录/子应用程序,并在每个子应用程序中使用所需的配置文件创建自己的 web.config。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文