Search code examples
asp.net-mvc-5sitecoreasp.net-membershipsitecore8

Membership for external users in Sitecore 8


I want to build a user registration store for Sitecore 8 website. The users will go in an external database.

  • Sitecore.NET 8.0 (rev. 150223)
  • .NET Framework 4.0.30319.18052
  • Razor Version=3.0.0.0
  • MVC 5.2.3.0

Asp.net MVC comes with the membership built in (AccountController, views, etc). I wanted to port this to my Sitecore project. The problem is Sitecore also uses membership internally. I know there is a way for getting membership working for the Sitecore 6.x versions through the switching providers described at Sitecore authenticate users against external membership database.

Is it still the same process for Sitecore 8?


Solution

  • I ended up not using the SimpleMembership and just going with Membership. I could not get the adapter that the thecodeking link mentions to work.

    This method is not properly documented. I just had to change the config files. I did not have to create a custom class that inherits from MembershipProvider.

    Web.config:

    In membership section,

    • change realProviderName to "switcher"
    • copy "sql" node and change name and connectionStringName to "external"

    In switchingProviders section,

    • add "external" node with domains "external"

    web.config:

    <membership defaultProvider="sitecore" hashAlgorithmType="SHA1">
      <providers>
        <clear />
        <!-- change realProviderName to "switcher" -->
        <add name="sitecore"
             type="Sitecore.Security.SitecoreMembershipProvider, Sitecore.Kernel"
             realProviderName="switcher"
             providerWildcard="%"
             raiseEvents="true"
             />
        <add name="sql"
             type="System.Web.Security.SqlMembershipProvider"
             connectionStringName="core"
             applicationName="sitecore"
             minRequiredPasswordLength="1"
             minRequiredNonalphanumericCharacters="0"
             requiresQuestionAndAnswer="false"
             requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="256"
             />
        <add name="switcher"
             type="Sitecore.Security.SwitchingMembershipProvider, Sitecore.Kernel"
             applicationName="sitecore"
             mappings="switchingProviders/membership"
             />
        <!-- copy "sql" node and change name and connectionStringName to "external" -->
        <add name="external"
             type="System.Web.Security.SqlMembershipProvider"
             connectionStringName="external"
             applicationName="sitecore"
             minRequiredPasswordLength="1"
             minRequiredNonalphanumericCharacters="0"
             requiresQuestionAndAnswer="false"
             requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="256"
             />
      </providers>
    </membership>
    
    <switchingProviders>
      <membership>
        <provider providerName="sql" storeFullNames="true" wildcard="%" domains="*" />
        <!-- add "external" node with domains "external" -->
        <provider providerName="external" storeFullNames="true" wildcard="%" domains="external" />
      </membership>
    </switchingProviders>
    

    ConnectionStrings.config:

    • add connection "external"

    config:

    <add name="external" connectionString="..." providerName="System.Data.SqlClient"/>
    

    Domains.config:

    • add domain "external"

    config:

    <domain name="external" ensureAnonymousUser="false" />
    

    Then use the "external" provider directly which saves user to external db. This is the key point.

    // uses "external" provider directly
    Membership.Providers["external"].CreateUser(...)
    

    Instead of this which saves to core db.

    // uses default provider
    Membership.CreateUser(...)