locked
http 400 error WCF restful service RRS feed

  • Question

  • Hello,

    In my wcf restful service, I m trying to make it available on https and having client to send me the username and password to call my service. I have read different articles and the way I got it is giving me http 400 error and I m not sure where the problem is.

    Any suggestions would be helpful.

    The config file is

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.web>
        <compilation debug="true" strict="false" explicit="true" targetFramework="4.0">
          <assemblies>
            <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
          </assemblies>
        </compilation>
      </system.web>
      <system.serviceModel>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true"  aspNetCompatibilityEnabled ="true"/>
        <standardEndpoints>
          <webHttpEndpoint>
            <standardEndpoint name="" helpEnabled ="true" automaticFormatSelectionEnabled ="true"></standardEndpoint>
          </webHttpEndpoint>
        </standardEndpoints>
        <bindings>
          <!--<wsHttpBinding>
            <binding name ="SimRestServiceBinding">
              <security mode ="Message">
                <message clientCredentialType ="UserName" />
              </security>
            </binding>
          </wsHttpBinding>-->
          <!--<webHttpBinding>
            <binding name="webBinding">
              <security mode="None">
              </security>
            </binding>
          </webHttpBinding>-->
          <basicHttpBinding>       
            <binding name = "httpBinding">
              <security mode ="TransportWithMessageCredential">
                <!--<transport clientCredentialType ="None" proxyCredentialType ="None"></transport>-->
                <message clientCredentialType ="UserName"/>
              </security>
            </binding>
          </basicHttpBinding>
        </bindings>
        <services>
          <service name ="SimRestServices.SimRestService" behaviorConfiguration="ServiceBehavior">
            <!-- Service Endpoints -->
            <endpoint address="" binding="basicHttpBinding" bindingConfiguration="httpBinding"
                       contract="SimRestServices.ISimRestService"></endpoint>
            <!--<endpoint address="" binding="webHttpBinding" bindingConfiguration="webBinding"
                       contract="SimRestServices.ISimRestService"></endpoint>-->
            <!-- Metadata Endpoints -->
            <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients  -->
            <!-- This endpoint doesnt use a secure binding and should be secured or removed before deployment -->
            <endpoint address ="mex" binding ="mexHttpsBinding" contract ="IMetadataExchange"></endpoint>
            <host>
              <baseAddresses>
                <add baseAddress ="https://www.mysite.com/simservices/"/>
              </baseAddresses>
            </host>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name = "ServiceBehavior">
              <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint 
    
    above before deployment -->
              <serviceMetadata httpGetEnabled="false" httpsGetEnabled ="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" />
              <serviceCredentials>
                <userNameAuthentication userNamePasswordValidationMode ="Custom"
                                         customUserNamePasswordValidatorType ="SimRestServices.UserNamePassValidator, 
    
    SimRestServices" />
              </serviceCredentials>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
      </system.webServer>
      <connectionStrings>
        <add name="Entities" connectionString=""  </connectionStrings>
    </configuration>


    • Edited by Me.Saqib Tuesday, December 9, 2014 6:35 PM
    Monday, December 1, 2014 8:47 PM

Answers

  • Hi Me.Saqib,

    It will be better if you can try to post the detailed error information here.

    >>client validation (custom authentication) and SSL WCF restful service

    Then it seems that you are using the WCF Restful service, but from your config file, you are using the basicHttpBinding, please try to use the WebHttpBinding for instead.

    Besides, please try to check the following two articles about how to create a Https WCF Rest Service with the username authentication.
    #Secure Self-Hosted WCF REST Services with a Custom UserNamePasswordValidator:
    https://www.develop.com/secure-self-hosted-wcf-rest-services-with-a-custom-usernamepasswordvalidator .

    #Creating a WCF RESTful Service And Secure It Using HTTPS Over SSL:
    http://www.allenconway.net/2012/05/creating-wcf-restful-service-and-secure.html .

    Best Regards,
    Amy Peng

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, December 4, 2014 5:05 AM
  •  

    The error was on this line

     <service name ="SimRestServices.SimRestService" behaviorConfiguration="ServiceBehavior">

    Somehow in restful/webapi service, you cant name your service as I did here (Namespace.ClassName)

    This is OK when using WCF Service, but not ok when using webapi/restful service.

    Also you right that webapi uses webhttpbinding and I had basichttpbinding.

    Also to solve my authentication problem, I converted my code to WCF (.svc ) instead of webapi and authentication also started to work.

     

    • Marked as answer by Me.Saqib Tuesday, December 9, 2014 6:34 PM
    Tuesday, December 9, 2014 6:34 PM

All replies

  • Hi Me.Saqib,

    It will be better if you can try to post the detailed error information here.

    >>client validation (custom authentication) and SSL WCF restful service

    Then it seems that you are using the WCF Restful service, but from your config file, you are using the basicHttpBinding, please try to use the WebHttpBinding for instead.

    Besides, please try to check the following two articles about how to create a Https WCF Rest Service with the username authentication.
    #Secure Self-Hosted WCF REST Services with a Custom UserNamePasswordValidator:
    https://www.develop.com/secure-self-hosted-wcf-rest-services-with-a-custom-usernamepasswordvalidator .

    #Creating a WCF RESTful Service And Secure It Using HTTPS Over SSL:
    http://www.allenconway.net/2012/05/creating-wcf-restful-service-and-secure.html .

    Best Regards,
    Amy Peng

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, December 4, 2014 5:05 AM
  •  

    The error was on this line

     <service name ="SimRestServices.SimRestService" behaviorConfiguration="ServiceBehavior">

    Somehow in restful/webapi service, you cant name your service as I did here (Namespace.ClassName)

    This is OK when using WCF Service, but not ok when using webapi/restful service.

    Also you right that webapi uses webhttpbinding and I had basichttpbinding.

    Also to solve my authentication problem, I converted my code to WCF (.svc ) instead of webapi and authentication also started to work.

     

    • Marked as answer by Me.Saqib Tuesday, December 9, 2014 6:34 PM
    Tuesday, December 9, 2014 6:34 PM