2010-08-12 34 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與所需的配置文件。