2010-08-12 92 views
3

我有多个配置文件提供商和配置文件类型:多种个性化配置提供

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> 

回答

3

我有多个配置文件和供应商解决了这个,使用下面的技巧:

第一:创建配置文件设定的一个基类。它不必包含所有的字段;他们共享相同的基类非常重要(我称之为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()检索该用户。

+0

对于这个问题,8年后还没有解决。 – Luc 2018-03-07 19:54:49

0

你不能有超过1个人在web.config中,但配置文件可以有多个供应商,以便也许你应该开始考虑改变你的架构只有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与所需的配置文件。