代码之家  ›  专栏  ›  技术社区  ›  Jan Jongboom

多个配置文件提供程序

  •  3
  • Jan Jongboom  · 技术社区  · 14 年前

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

    ProviderA 具有 ProfileA

    ProviderB 具有 ProfileB

    它们都使用不同的数据库。我想说:

    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>
    
    2 回复  |  直到 14 年前
        1
  •  3
  •   Jan Jongboom    14 年前

    我已经通过多个配置文件和提供者解决了这一问题,使用了以下黑客:

    首先:为您的配置文件创建一个基类。它不必包含所有的字段;重要的是它们共享同一个基类(我称之为 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();
    }
    

    现在我们已经在according数据库中创建了一个用户和一个概要文件。您可以通过 membershipProvider 以及通过 ProfileManager.FindProfilesByUserName() .

        2
  •  0
  •   Ivan Ferić    14 年前

    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>
    

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

    推荐文章