Having made the transition to using MVC architecture for all my new applications I recently came across a situation where I needed to use the WebMatrix membership provider within my WCF services. It wasn’t a trivial process so I thought I would document it here.
So how do you get access to the SimpleMembershipProvider in WCF?
First add the references to the WebMatrix dlls
Second add the Web.Config reference in system.web as such
<membership defaultProvider="SimpleMembershipProvider"> <providers> <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" connectionStringName="MyApplicationConnectionString" applicationName="MyApplicationName" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" passwordFormat="Hashed" /> </providers> </membership>
Making sure you select your defaultProvider as SimpleMembershipProvider.
Lastly make sure the required assemblies are referenced in the web.config system.web.compilation.assemblies section as such.
<compilation debug="true" targetFramework="4.5"> <assemblies> <add assembly="WebMatrix.Data, Version=22.214.171.124, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> <add assembly="WebMatrix.WebData, Version=126.96.36.199, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </assemblies> </compilation>
and at that point you should be able to do this.
SimpleMembershipProvider provider = (SimpleMembershipProvider)System.Web.Security.Membership.Provider;
and use the provider now as you as you normally would i.e. ValidateUser(), ChangePassword() etc…
However at this point I ran into an issue where the following error appeared. “Parser Error Message: This method cannot be called during the application’s pre-start initialization stage.”. The solution to this problem was the following StackOverflow post and adding the these into my appSettings.
<appSettings> <add key="autoFormsAuthentication" value="true"/> </appSettings>
Which successfully fixed the problem and everything worked at that point.