Search code examples
wcfwcf-bindingwcf-security

How to configure webHttpBinding using user name/password authentication


I am working on a WCF service with webHttpBinding for json ajax calls. The service works fine until I turned on the security part. Here is my web.config.

      <serviceCredentials >
        <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="UserNamePasswordValidator, WebServices" />
      </serviceCredentials>
      <serviceAuthorization principalPermissionMode="Custom">
        <authorizationPolicies>
          <add policyType="AuthorizationPolicy, WebServices" />
        </authorizationPolicies>
      </serviceAuthorization>
      <!---->
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="WebHttpBehaviour">
      <enableWebScript />
      <webHttp automaticFormatSelectionEnabled="false" defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Json" helpEnabled="true" />
    </behavior>
  </endpointBehaviors>
</behaviors>

Here is my javascript call.

$.ajax({
       headers: {
                "Authorization": "Basic " + Base64.encode('John:Doe')
            },
            type: "POST",
            url: "https://localhost/StatusService.svc/CheckStatus",
            data: JSON.stringify({"companyName":"test"}),
            contentType: "text/json; charset=utf-8",
            dataType: "json",
            processdata: false,
            success: function (data) {
                alert('ok!');
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(textStatus + ' / ' + errorThrown);
            }
        });

I got error message: object error when I do the ajax call. Is there anything I am doing wrong? Thanks.


Solution

  • Find the following solution. Works great. http://sameproblemmorecode.blogspot.com/2011/10/creating-secure-restfull-wcf-service.html