【问题标题】:Wcf Basic authenticationWcf 基本身份验证
【发布时间】:2012-01-03 11:13:58
【问题描述】:

通过简单的测试 Wcf 服务使用基本身份验证时遇到一些问题。我遇到了一个例外:

无法激活请求的服务“http://qld-tgower/test/Service.svc”。有关详细信息,请参阅 > 服务器的诊断跟踪日志。

在跟踪日志中显示:

在主机('Basic')上配置的身份验证方案不允许在绑定'BasicHttpBinding'('Anonymous')上配置的那些。请确保将 SecurityMode 设置为 Transport 或 TransportCredentialOnly。此外,这可以通过 IIS 管理工具更改此应用程序的身份验证方案来解决,通过 ServiceHost.Authentication.AuthenticationSchemes 属性,在应用程序配置文件中的 元素,通过更新绑定上的 ClientCredentialType 属性,或通过调整 HttpTransportBindingElement 上的 AuthenticationScheme 属性。

但是当我使用不正确的用户名和密码时,我不明白它说 IS 使用基本身份验证?

使用客户端身份验证方案“基本”未授权 HTTP 请求。从服务器收到的身份验证标头是 'Basic realm="qld-tgower"'。

这是我的 web.config 详细信息

<system.serviceModel>
<services>
  <service name="WcfService"
      behaviorConfiguration="Behavior">
    <endpoint address="http://QLD-TGOWER/test/Service.svc"
              binding="basicHttpBinding"
              bindingConfiguration="httpBinding"
              contract="IService" />
  </service>
</services>
<diagnostics>
  <endToEndTracing activityTracing="false" messageFlowTracing="true" propagateActivity="true"></endToEndTracing>
</diagnostics>
<bindings>
  <basicHttpBinding>
    <binding name="httpBinding">
      <security mode="TransportCredentialOnly">
        <transport  clientCredentialType="Basic" proxyCredentialType="Basic">
        </transport>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>

这是我的 App.config

<system.serviceModel>
    <diagnostics>
      <endToEndTracing activityTracing="true" />
      <messageLogging logMessagesAtTransportLevel="true" />
    </diagnostics>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService" >
          <security mode="TransportCredentialOnly">

            <transport clientCredentialType="Basic" proxyCredentialType="Basic"></transport>
            <message clientCredentialType="UserName" />
          </security>

        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://QLD-TGOWER/test/Service.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference1.IService"
        name="BasicHttpBinding_IService" />
    </client>
</system.serviceModel>

我的测试应用程序

private static void Main(string[] args)
{
    var proxy = new ServiceClient("BasicHttpBinding_IService");
    var clientCredentials = proxy.ClientCredentials;
    clientCredentials.UserName.UserName = "username";
    clientCredentials.UserName.Password = "password";
    var res = proxy.GetData(1);
    Console.WriteLine(res);
    Console.WriteLine("Done");
    Console.ReadKey(true);
}

还有我的服务

public class Service : IService
{

   public string GetData(int value)
   {
       return string.Format("You entered: {0}", value);
   }
}

这里有什么我遗漏的吗?

【问题讨论】:

    标签: wcf basichttpbinding


    【解决方案1】:

    更改服务的名称和合同以包含命名空间。

    另外,删除端点地址(将其设置为“”)并且不要在传输标记中包含 proxyCredentialType。

    web.config 的最终结果应该是这样的

      <system.serviceModel>
    
        <services>
          <service name="MyNameSpace.MyService" behaviorConfiguration="asdf">
            <endpoint address="" binding="basicHttpBinding" 
                bindingConfiguration="httpBinding" contract="MyNameSpace.IMyService" />
          </service>
        </services>
    
        <diagnostics>
          <endToEndTracing activityTracing="true" messageFlowTracing="true" 
              propagateActivity="true">
          </endToEndTracing>
        </diagnostics>
    
        <bindings>
          <basicHttpBinding>
            <binding name="httpBinding">
              <security mode="TransportCredentialOnly">
                <transport clientCredentialType="Basic" />
              </security>
            </binding>
          </basicHttpBinding>
        </bindings>
    
        <behaviors>
          <serviceBehaviors>
            <behavior name="asdf">
              <!-- To avoid disclosing metadata information, set the value below to 
                   false and remove the metadata endpoint above before deployment -->
              <serviceMetadata httpGetEnabled="true" />
              <!-- To receive exception details in faults for debugging purposes, 
                   set the value below to true.  Set to false before deployment to avoid 
                   disclosing exception information -->
              <serviceDebug  includeExceptionDetailInFaults="true" />
    
            </behavior>
          </serviceBehaviors>
        </behaviors>
    
        <serviceHostingEnvironment multipleSiteBindingsEnabled="false"/>
    
      </system.serviceModel>
    

    【讨论】:

      【解决方案2】:

      尝试客户端和服务器配置

      <basicHttpBinding>
          <binding name="BasicHttpBinding_IService">
              <security mode="TransportCredentialOnly">
                  <transport clientCredentialType="Basic" />
              </security>
          </binding>
      </basicHttpBinding>
      

      安装/启用基本身份验证

      您可能还需要在 IIS 中安装和应用基本身份验证。

      转到“程序和功能”/“打开/关闭 Windows 功能”。 在 IIS 和安全性下启用“基本身份验证”。

      我关闭并打开了 IIS 控制台,并且能够在身份验证设置下启用它。

      当然,如果用于开发测试,它会警告您没有 SSL 证书。

      【讨论】:

      • 谢谢,这已经修复了身份验证错误,但现在服务只是进入故障状态。跟踪日志中没有任何内容表明原因。
      • 你的答案应该被标记为正确答案,干杯老兄
      【解决方案3】:

      您不能通过不安全的连接使用用户名身份验证

      您可以使用安全传输(例如 SSL)或消息加密(使用证书)来保护消息

      我过去曾使用ClearUsernameBinding 取得了巨大的成功,但我不建议在生产中使用它。我使用它是为了让我的所有身份验证代码保持不变,而无需在开发/测试环境中使用 SSL,但只需更改配置即可使其与 SSL 一起使用。

      注意:自定义绑定并不完美,我不得不对其进行一些更改以启用某些配置更改。

      【讨论】:

        【解决方案4】:

        这就是为我解决问题的方法:

        <bindings>
          <basicHttpBinding>
            <binding>
              <security mode="TransportCredentialOnly">
                <transport clientCredentialType="Windows" />
              </security>
            </binding>
          </basicHttpBinding>
        </bindings>
        

        参考见: https://msdn.microsoft.com/en-gb/library/ff648505.aspx

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-18
          • 2011-10-25
          • 2011-08-17
          • 2011-11-03
          • 1970-01-01
          • 2011-12-06
          相关资源
          最近更新 更多