WCF REST Service with Windows Authentication and S

2019-08-10 05:18发布

问题:

I have WCF Rest Service that is being consumed by android, iOS and asp.net web application. all endpoint methods return json format so I use webHttpBinding. Problems started when I secured the service by activating Windows authentication(disabled Anonymous before that). When I try to add Service reference from the asp.net web application I receive the following error:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate,NTLM'.
The remote server returned an error: (401) Unauthorized.

When I call endpoints in browser windows dialog is being showed with username and pass and everything works fine, but When I try to add the reference I receive the error. I'm also using SSL and the service has only https address.

web.config wcf rest service:

<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" executionTimeout="90"        maxRequestLength="20000" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="8192" />
    <authentication mode="Windows" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<bindings>
  <webHttpBinding>
    <binding name="WebHttpEndpointBinding"   maxReceivedMessageSize="200000000" maxBufferPoolSize="200000000">
      <readerQuotas maxDepth="32" maxArrayLength="2000000000"   maxStringContentLength="2000000000" />
      <security mode="Transport">
      <transport clientCredentialType="Windows"  /> 
      </security>
    </binding>
  </webHttpBinding>  
</bindings>
<services>
  <service name="restSrvService.restSrvRestService"   behaviorConfiguration="serviceBehavior">
    <endpoint address="" name="webHttpEndpoint" binding="webHttpBinding"   bindingConfiguration="WebHttpEndpointBinding" behaviorConfiguration="web"   contract="restSrvService.IrestSrvRestService">   
    </endpoint> 
 <endpoint address="mex" binding="mexHttpBinding"  contract="IMetadataExchange"/>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="serviceBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
     <serviceAuthenticationManager authenticationSchemes="IntegratedWindowsAuthentication" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web">
      <endpointDiscovery enabled="true">
      </endpointDiscovery>
      <webHttp  />
    </behavior>
  </endpointBehaviors>
</behaviors>
</system.serviceModel>

web.config web application:

    <system.webServer>
      <modules runAllManagedModulesForAllRequests="true"></modules>
    </system.webServer>
    <system.serviceModel>
    <bindings>
     <webHttpBinding>
       <binding name="WebHttpEndpointBinding" bypassProxyOnLocal="true" useDefaultWebProxy="true" maxReceivedMessageSize="200000000" maxBufferPoolSize="200000000">
         <readerQuotas maxDepth="32" maxArrayLength="2000000000" maxStringContentLength="2000000000" />
           <security mode="Transport">
             <transport clientCredentialType="Windows"></transport>
           </security>
       </binding>
     </webHttpBinding>
    </bindings>
    <client>

      <endpoint address="https://serviceAddress/restSrvRestService.svc/"
        binding="webHttpBinding" bindingConfiguration="WebHttpEndpointBinding"
        contract="restSrvService.IrestSrvRestService"  behaviorConfiguration="webEndpoint" name="WebHttpEndpoint">
        <identity>
        </identity>
      </endpoint>
    </client>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webEndpoint">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>