Search code examples
wcfauthenticationumbracomembership-provider

Host custom WCF service with authentication within Umbraco


I've created a custom WCF service within Umbraco. The service resides in the Service folder and seems to be working fine (I can call it and it responds appropriately). Now I want the users to authenticate themselves when they call the service.

To do this I've added these lines into the web.config:

<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
 <bindings>
  <webHttpBinding>
    <binding name="RaceManBinding">
      <security mode="None" />
    </binding>
  </webHttpBinding>
</bindings>
<services>
  <service name="RaceManagerAdmin.RaceManDataService" behaviorConfiguration="RaceManBehavior">

    <endpoint address=""
              binding="webHttpBinding"
              contract="System.Data.Services.IRequestHandler" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="RaceManBehavior">
      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="MembershipProvider"
                                membershipProviderName="UmbracoMembershipProvider" />
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>

When my service is called it should use the Umbraco membership provider to authenticate the users.

My client specificies this by creating a network credential object, like this:

var a = new RaceEntities(new Uri("http://localhost:40406/umbraco/Webservices/RaceManDataService.svc")) { Credentials = new NetworkCredential("admin", "secret") };

When I inspect the HTTPContext.Current I don't see any authenticated users.

What am I doing wrong?

Frederik


Solution

  • You'll need to enable ASP.Net Compatibility in order to access the identity via the HttpContext:

    <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    </system.serviceModel>
    

    The answer to this question mentions an alternate method: How to access HttpContext.Current.User.Username in WCF service