Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HTTP请求未经客户端身份验证方案';匿名';?_C#_Asp.net_Wcf_Restful Authentication_Basichttpbinding - Fatal编程技术网

C# HTTP请求未经客户端身份验证方案';匿名';?

C# HTTP请求未经客户端身份验证方案';匿名';?,c#,asp.net,wcf,restful-authentication,basichttpbinding,C#,Asp.net,Wcf,Restful Authentication,Basichttpbinding,当试图从我的web客户端应用程序调用RESTful服务(我在RESTful服务的示例中使用了UserNameAuthenticator)时,出现了如下错误 The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic Realm'. 客户端CS代码 BasicHttpBind

当试图从我的web客户端应用程序调用RESTful服务(我在RESTful服务的示例中使用了UserNameAuthenticator)时,出现了如下错误

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic Realm'.
客户端CS代码

BasicHttpBinding binding = new BasicHttpBinding();
binding.SendTimeout = TimeSpan.FromSeconds(25);
binding.Security.Transport.ClientCredentialType =
HttpClientCredentialType.Basic;
EndpointAddress address = new  EndpointAddress("http://localhost:12229/RestServiceImpl.svc");
ChannelFactory<RestService.IRestServiceImpl> factory =
new ChannelFactory<RestService.IRestServiceImpl>(binding, address);
RestService.IRestServiceImpl channel = factory.CreateChannel();
channel.GetStudent();
BasicHttpBinding=new BasicHttpBinding();
binding.SendTimeout=TimeSpan.FromSeconds(25);
binding.Security.Transport.ClientCredentialType=
HttpClientCredentialType.Basic;
EndpointAddress地址=新的EndpointAddress(“http://localhost:12229/RestServiceImpl.svc");
渠道工厂=
新工厂(绑定、地址);
RestService.IRestServiceImpl通道=工厂.CreateChannel();
channel.GetStudent();
客户端Web.config

<system.serviceModel>
    <services>
      <service name="RestService.RestServiceImpl">
        <endpoint address="http://localhost:12229/RestServiceImpl.svc"
          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ExternalSystemsService_v1Interface"
          contract="RestService.IRestServiceImpl"
          name="ExternalSystemsService_v1Port" />
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ExternalSystemsService_v1Interface"
                         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="TransportCredentialOnly">
            <transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>
 <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="webHttpTransportSecurity">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
          </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="RestService.RestServiceImpl">
        <endpoint name="ExternalSystemsService_v1Port" address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ExternalSystemsService_v1Interface" contract="RestService.IRestServiceImpl"></endpoint>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="SecureRESTSvcTestBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="RESTfulSecuritySH.CustomUserNameValidator, RESTfulSecuritySH" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

和RESTful服务Web.config

<system.serviceModel>
    <services>
      <service name="RestService.RestServiceImpl">
        <endpoint address="http://localhost:12229/RestServiceImpl.svc"
          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ExternalSystemsService_v1Interface"
          contract="RestService.IRestServiceImpl"
          name="ExternalSystemsService_v1Port" />
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ExternalSystemsService_v1Interface"
                         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="TransportCredentialOnly">
            <transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>
 <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="webHttpTransportSecurity">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
          </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="RestService.RestServiceImpl">
        <endpoint name="ExternalSystemsService_v1Port" address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ExternalSystemsService_v1Interface" contract="RestService.IRestServiceImpl"></endpoint>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="SecureRESTSvcTestBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="RESTfulSecuritySH.CustomUserNameValidator, RESTfulSecuritySH" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>


有什么建议吗?

我想指出的一点是,在客户端CS代码中,您通过编程方式设置了传输的客户端凭据类型:

binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
但是在客户端和服务器配置文件中,您都设置了消息客户端凭据类型。请注意,传输元素的clientCredentialType属性如何设置为“无”,消息元素的clientCredentialType如何设置为“用户名”:


我总是尝试在开发中关闭安全性,然后慢慢地重新打开它


这也是一个新的开发项目吗?我很好奇为什么您要使用WCF over ASP.NET Web API来实现RESTful服务。

我注意到的一点是,在客户端CS代码中,您可以通过编程方式设置传输的客户端凭据类型:

binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
但是在客户端和服务器配置文件中,您都设置了消息客户端凭据类型。请注意,传输元素的clientCredentialType属性如何设置为“无”,消息元素的clientCredentialType如何设置为“用户名”:


我总是尝试在开发中关闭安全性,然后慢慢地重新打开它


这也是一个新的开发项目吗?我很好奇为什么您要使用WCF over ASP.NET Web API来实现RESTful服务。

将完全忽略
.config
,因为OP正在实例化
BasicHttpBinding
本身,与使用默认构造函数中的工厂方法作为
ChannelFactory
相反,
.config
将被完全忽略,因为OP正在实例化
BasicHttpBinding
本身,与使用默认构造函数中的工厂方法(用于
ChannelFactory
)相反,在构建
BasicHttpBinding
时,您根本没有使用
.config
。因此,对于Auth,您所做的只是设置身份验证协议,但尚未设置要针对其进行身份验证的凭据。使用将绑定名称作为
字符串的
ChannelFactory
构造函数,或者在实例化的
BasicHttpBinding
中设置凭据。在构造
BasicHttpBinding
时,根本不使用
.config
。因此,对于Auth,您所做的只是设置身份验证协议,但尚未设置要针对其进行身份验证的凭据。使用将绑定名称作为
字符串的
ChannelFactory
构造函数,或者在实例化的
BasicHttpBinding
中设置凭据。