Search code examples
wcfmembership-provider

Using custom membership provider in a wcf service error


I have an asp.net mvc 4 web application where I have created a custom membership provider (inheriting from MembershipProvider base class). In the same solution but in another project I have a wcf service where I want to authenticate a user using the ValidateUser method from my membership provider (which is called KinematMembershipProvider)

This is my interface of the service

[ServiceContract]
public interface IKinematService
{
    [OperationContract]
    bool AuthenticateUser(string username, string password);
}

and the actual service

public class KinematService : IKinematService
{
    public bool AuthenticateUser(string username, string password)
    {
        KinematMembershipProvider membershipProvider = (KinematMembershipProvider)Membership.Providers["KinematMembershipProvider"];
        return membershipProvider.ValidateUser(username, password);
    }
}

and this is my app.config file

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!--configSections-->
  <configSections>
    <section name="entityFramework"
             type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <!--appSettings-->
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <!--connectionStrings-->
  <connectionStrings>
    <add name="KinematDbContext" providerName="System.Data.SqlClient"
         connectionString="Data Source=.;Initial Catalog=KinematDatabase;Integrated Security=SSPI;" />
  </connectionStrings>
  <!--system.web-->
  <system.web>
    <!--compilation-->
    <compilation debug="true" />
    <!--membership-->
    <membership defaultProvider="KinematMembershipProvider">
      <!--providers-->
      <providers>
        <add name="KinematMembershipProvider" type="Kinemat.Web.Infrastructure.KinematMembershipProvider"
             connectionStringName="KinematDbContext" enablePasswordRetrieval="true" enablePasswordReset="true"
             requiresQuestionAndAnswer="false" requiresUniqueEmail="true" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6"
             minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" passwordFormat="Encrypted" />
      </providers>
    </membership>
  </system.web>
  <!--system.serviceModel-->
  <system.serviceModel>
    <!--services-->
    <services>
      <service name="Kinemat.WebServices.KinematService" behaviorConfiguration="ProviderServiceBehavior">
        <endpoint address="http://localhost:52174/WcfServices/Kinemat" binding="wsHttpBinding" bindingConfiguration="MembershipBinding" 
                  contract="Kinemat.WebServices.IKinematService">
        </endpoint>
      </service>
    </services>
    <!--bindings-->
    <bindings>
      <wsHttpBinding>
        <binding name="MembershipBinding">
          <security mode ="Message">
            <message clientCredentialType ="UserName" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <!--behaviors-->
    <behaviors>
      <!--serviceBehaviors-->
      <serviceBehaviors>
        <behavior name="ProviderServiceBehavior">
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="MembershipProvider"
                                    membershipProviderName="KinematMembershipProvider" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <!--entityFramework-->
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

When I run my service I get the following error System.TypeLoadException: Could not load type 'Kinemat.Web.Infrastructure.KinematMembershipProvider' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

I cannot figure out why it is trying to search in System.Web assembly. Please do you have any suggestion? Please ask for any additional information if you need. Thank you.


Solution

  • It is trying to find it in the System.Web assembly because you have not specified in the configuration where it should actually search for your provider. Try amending your configuration to include the assembly name like below:

    <providers>
        <add name="KinematMembershipProvider" type="Kinemat.Web.Infrastructure.KinematMembershipProvider, Assembly.Name.Where.Provider.Resides" .../>
    </providers>