如何将用户凭据从WebClient传递到WCF REST服务?

8 security wcf webclient webhttpbinding

我正在尝试公开WCT REST服务,只有拥有有效用户名和密码的用户才能访问它.用户名和密码存储在SQL数据库中.

这是服务合同:

public interface IDataService
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    byte[] GetData(double startTime, double endTime);
}
Run Code Online (Sandbox Code Playgroud)

这是WCF配置:

<bindings>
  <webHttpBinding>
    <binding name="SecureBinding">
      <security mode="Transport">
        <transport clientCredentialType="Basic"/>
      </security>
    </binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="DataServiceBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceCredentials>
        <userNameAuthentication
           userNamePasswordValidationMode="Custom"
           customUserNamePasswordValidatorType=
                 "CustomValidator, WCFHost" />
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>
<services>
  <service behaviorConfiguration="DataServiceBehavior" name="DataService">
    <endpoint address="" binding="webHttpBinding"
              bindingConfiguration="SecureBinding" contract="IDataService" />
  </service>
</services>
Run Code Online (Sandbox Code Playgroud)

我通过Silverlight应用程序中的WebClient类访问该服务.但是,我无法弄清楚如何将用户凭据传递给服务.我为client.Credentials尝试了各种值,但它们似乎都没有在我的自定义验证器中触发代码.我收到以下错误:基础连接已关闭:发送时发生意外错误.

这是我尝试过的一些示例代码:

   WebClient client = new WebClient();
   client.Credentials = new NetworkCredential("name", "password", "domain");
   client.OpenReadCompleted += new OpenReadCompletedEventHandler(GetData);
   client.OpenReadAsync(new Uri(uriString));
Run Code Online (Sandbox Code Playgroud)

如果我将安全模式设置为None,则整个过程都有效.我还尝试了其他clientCredentialType值,但没有一个工作.我还自行托管了WCF服务,以消除与IIS尝试在服务获得机会之前对用户进行身份验证相关的问题.

任何关于潜在问题的评论都会受到高度赞赏.谢谢.

更新:感谢穆罕默德的出色建议.这是我的跟踪配置:

 <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
      <source name="System.IdentityModel" switchValue="Information, 
               ActivityTracing" propagateActivity="true">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xml"
           type="System.Diagnostics.XmlWriterTraceListener"
           initializeData="c:\Traces.svclog" />
    </sharedListeners>
  </system.diagnostics>
Run Code Online (Sandbox Code Playgroud)

但是我没有看到来自Silverlight客户端的任何消息.至于https vs http,我使用https如下:

string baseAddress = "https://localhost:6600/";
_webServiceHost = new WebServiceHost(typeof(DataServices), 
                new Uri(baseAddress));
_webServiceHost.Open();
Run Code Online (Sandbox Code Playgroud)

但是,我没有配置任何SSL证书.这是问题吗?

Meh*_*ras 0

首先,您可能希望将 WCF 跟踪添加到您的服务中以获取更多详细信息。其次,我认为问题可能是您以明文形式发送用户凭据,而 WCF 不允许通过不安全的传输通道以明文形式传输用户凭据。因此,请尝试使用 https 或指定加密算法来通过 http 保护用户凭据。