【问题标题】:The HTTP request is unauthorized with client authentication scheme 'Basic'. The authentication header received from the server was 'Basic realm="pc"'HTTP 请求未经客户端身份验证方案“基本”授权。从服务器收到的身份验证标头是“Basic realm="pc"”
【发布时间】:2009-12-19 00:12:38
【问题描述】:

服务器:

<system.serviceModel>
    <services>
        <service name="Service" behaviorConfiguration="md">
            <!-- Service Endpoints -->
            <endpoint address="SslService" binding="basicHttpBinding" bindingConfiguration="security" contract="IService"/>
            <host>
                <baseAddresses>
                    <add baseAddress="https://pc:8080/Service.svc"/>
                </baseAddresses>
            </host>
        </service>
    </services>
    <bindings>
        <basicHttpBinding>
            <binding name="security">
                <security mode="Transport">
                    <transport clientCredentialType="Basic"/>
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <behaviors>
        <serviceBehaviors>
            <behavior name="md">
      <serviceCredentials>
        <userNameAuthentication
          userNamePasswordValidationMode="Custom"
          customUserNamePasswordValidatorType="ClassLibrary1.CustomUserNameValidator, ClassLibrary1" />
      </serviceCredentials>
                <serviceMetadata httpsGetEnabled="true"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

ClassLibrary1.CustomUserNameValidato:

public class CustomUserNameValidator : System.IdentityModel.Selectors.UserNamePasswordValidator
    {
        public override void Validate(string userName, string password)
        {
            if (userName != "111" || password != "111")
            {

                throw new System.ServiceModel.FaultException("Unknown username or incorrect password");
            }
        }
    }

客户:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="Transport">
                    <transport clientCredentialType="Basic" proxyCredentialType="Basic" realm="">
                        <extendedProtectionPolicy policyEnforcement="Never" />
                    </transport>
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="https://pc:8080/Service.svc/SslService" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference1.IService"
            name="BasicHttpBinding_IService" />
    </client>
</system.serviceModel>

ServiceReference1.ServiceClient s = new WindowsFormsApplication1.ServiceReference1.ServiceClient();

s.ClientCredentials.UserName.UserName = "111";
s.ClientCredentials.UserName.UserName = "111";
MessageBox.Show(s.GetData(3)); // <---- ERROR

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

【问题讨论】:

    标签: wcf


    【解决方案1】:

    我创建了一个这样的客户端:

    using (var client = new Client())
        {
    
        client.ClientCredentials.UserName.UserName = <username>;
        client.ClientCredentials.UserName.Password = **<WRONG_PASSWORD>**;
    ...
        }
    

    我的绑定的安全部分如下所示:

              <security mode="Transport">
                <transport clientCredentialType="Basic" proxyCredentialType="Basic" realm="" />
              </security>
    

    我看到这个错误又回来了。更正密码后,一切正常。

    【讨论】:

    • 同意这一点。错误的密码可能是导致此错误消息的原因。
    • 谢谢。您回答的网络配置部分解决了我的问题。;)
    【解决方案2】:

    我假设您在 IIS 上托管您的服务主机。那么问题是 IIS 在 WCF 框架之前拦截了 https 请求并执行 IIS 级别的身份验证,并且您的自定义验证器有机会启动。

    在您的示例中,IIS 实际上会在运行 IIS 的服务器上查找密码为“111”的本地用户“111”。尝试在服务器上创建这个用户,你可能会得到不同的结果。

    一种解决方案是将您的 WCF 服务主机托管在其他地方,例如在 Windows 服务中。另一种解决方案是将您的安全方案更改为 TransportWithMessageCredential。最后,您可以查看这个 OSS http 模块:Custom Basic Authentication for IIS - 似乎可以满足我们的需要。

    【讨论】:

      【解决方案3】:

      尝试发送用户名和密码,而不是使用基本身份验证的 http(这会使 IIS 尴尬),但只能在具有以下方案的肥皂消息头中发送:

      <binding name="...">
              <security mode="TransportWithMessageCredential" >
                 <message clientCredentialType="UserName" />
              </security>
      </binding>
      

      How to: Use Transport Security and Message Credentials

      也许您还需要另外指定&lt;transport clientCredentialType="None"&gt;

      【讨论】:

        【解决方案4】:

        我在这里发布了一个答案:Can not call web service with basic authentication using WCF 传输clientcredentialTypeTransportCredentialOnly

        【讨论】:

          【解决方案5】:

          看起来你设置了两次用户名而不是用户名和密码。

          当您具有基本身份验证并且您没有在请求中发送用户名和密码时,您会收到质询响应。

          【讨论】:

            【解决方案6】:

            我应用了上面提到的所有东西,但我的问题没有解决。

            在我的情况下,这是由于代理服务器而发生的。然后我删除了所有代理并运行我的网络服务。然后它工作正常。

            希望你会这样!!!!!!

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-12-10
              • 2014-12-07
              • 1970-01-01
              • 2019-05-09
              • 1970-01-01
              • 2016-07-15
              • 2011-12-02
              • 1970-01-01
              相关资源
              最近更新 更多