如何使用用户名/密码身份验证配置webHttpBinding

时间:2012-11-15 15:10:46

标签: wcf wcf-binding wcf-security

我正在使用webHttpBinding为json ajax调用创建一个WCF服务。该服务正常,直到我打开安全部分。这是我的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>

这是我的javascript通话。   

$.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);
            }
        });

我收到错误消息:当我执行ajax调用时出现对象错误。有什么我做错了吗?感谢。

1 个答案:

答案 0 :(得分:3)