4

我有一个配置为使用传输安全性和基本身份验证的 WCF 服务。

该服务托管在 iiexpress withing vs2010 中。

我能够从我的客户端代码连接,但总是收到:

"The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm=realm'."

这有一个内部例外:

"The remote server returned an error: (401) Unauthorized."

类似于Can not call web service with basic authentication using WCF虽然我的客户端代码已经具有答案中列出的设置。

我还按照IIS/ASP.NET 中针对非 Windows 帐户的 HTTP 基本身份验证(第 3 部分 - 添加 WCF 支持)和上一篇博客来设置模块和 IAuthorizationPolicy 类。

IISExpress 配置为经典模式,禁用匿名和 Windows 身份验证并启用 SSL。

客户端配置:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="NotificationHttpBinding">
          <security mode="Transport">
            <transport clientCredentialType="Basic" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://localhost/NotificationService.svc"
         binding="basicHttpBinding" bindingConfiguration="NotificationHttpBinding"
         contract="NotificationPortType" name="BasicHttpBinding_NotificationPortType" />
    </client>
  </system.serviceModel>

服务配置:

<system.serviceModel>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

    <services>
      <service name="Notification.NotificationService" behaviorConfiguration="NotificationServiceBehavior">
        <endpoint binding="basicHttpBinding" contract="NotificationPortType" bindingConfiguration="NotificationHttpBinding" >
        </endpoint>
       </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="NotificationServiceBehavior">
          <serviceMetadata />
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <serviceAuthorization>
            <authorizationPolicies>
              <add policyType="Notification.HttpContextIdentityPolicy, Notification" />
            </authorizationPolicies>
          </serviceAuthorization>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <bindings>
      <basicHttpBinding>
        <binding name="NotificationHttpBinding">
          <security mode="Transport">
            <transport clientCredentialType="Basic" />
          </security>

        </binding>
      </basicHttpBinding>
    </bindings>

  </system.serviceModel>


  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

  <system.web>

    <httpModules>
      <add name="CustomBasicAuthentication" type="Notification.CustomBasicAuthenticationModule, Notification"/>
    </httpModules>

    <membership defaultProvider="SampleProvider">
      <providers>
        <add name="SampleProvider"  type="Notification.HardcodedSecurityProviders, Notification" />
      </providers>
    </membership>

  </system.web>

客户端代码没什么大不了的:

static void Main(string[] args)
        {
            ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { return true; };

            NotificationPortTypeClient client = new NotificationPortTypeClient("BasicHttpBinding_NotificationPortType");

            client.ClientCredentials.UserName.UserName = "Test";
            client.ClientCredentials.UserName.Password = "PWD";


            client.sendNotification(new NotificationRequest());
        }

或者 ,如果有人可以向我展示如何使用 IIS6 托管服务 WCF 的替代方法,该服务使用基本的 http 身份验证同时需要 SSL (https),​​我会很高兴的!


更新 似乎这一直是我的罪魁祸首: 避免 http 401 往返

但是,我发现我的模块可以正常触发(在集成模式下),但随后出现服务错误,告诉我需要基本集成,但未在主机上启用。

打开 iisexpress applicationhost.config 文件,果然我发现:

<section name="basicAuthentication" overrideModeDefault="Deny" />

其次是

<basicAuthentication enabled="false" />

再向下

我已将这些更改为<section name="basicAuthentication" overrideModeDefault="Allow" />

并尝试在我的 web.config 中启用...没有骰子:(

4

1 回答 1

-2

你需要使用WSHttpBinding.

这里有一个完整的示例。

于 2011-04-07T12:40:56.990 回答