본문으로 바로가기

c# restful api 개발시 http, https 셋팅

category 프로그램&코딩 2018. 12. 12. 14:09
반응형

c# rest api 개발시 http, https 셋팅


c# 에서 restful api 를 개발해 놓았고, 테스트까지 잘 끝냈는데,


스테이지 or 운영(라이브) 반영시 https 라서 404 에러가 나거나 501 에러 등등이 떨어지는 경우가 있다.


이럴 경우 web.config 설정의 문제로..


개발 http 일 경우 셋팅은 아래와 같이.. soap, basic 의 경우 동일하게 셋팅..


 <system.serviceModel>

    

    <services>

      <service name="ABCService" behaviorConfiguration="ServiceBehavior">

          <endpoint address="rest" binding="webHttpBinding" contract="ABCService" behaviorConfiguration="web" />

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

        <behavior name="ABCServiceBehavior">

          <serviceMetadata httpGetEnabled="true"/>

          <serviceDebug includeExceptionDetailInFaults="true"/>

        </behavior>

      </serviceBehaviors>

      <endpointBehaviors>

        <behavior name="web">

          <webHttp />

        </behavior>

      </endpointBehaviors>

    </behaviors>

    <bindings>

      <basicHttpBinding>

        <binding name="basicBinding" closeTimeout="00:01:00" openTimeout="00:01:00"

         receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"

         bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"

         maxBufferPoolSize="524288" maxBufferSize="65536" maxReceivedMessageSize="65536"

         textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"

         messageEncoding="Text">

          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"

           maxBytesPerRead="4096" maxNameTableCharCount="16384" />

          <security mode="None">

            <transport clientCredentialType="None" proxyCredentialType="None" realm="" />

            <message clientCredentialType="UserName" algorithmSuite="Default" />

          </security>

        </binding>

      </basicHttpBinding>

      <wsHttpBinding>

        <binding name="WSHttpBinding" closeTimeout="00:01:00"

         openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"

         bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"

         maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"

         textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">

          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"

           maxBytesPerRead="4096" maxNameTableCharCount="16384" />

          <reliableSession ordered="true" inactivityTimeout="00:10:00"

           enabled="false" />

          <security mode="None">

            <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />

            <message clientCredentialType="Windows" negotiateServiceCredential="true" establishSecurityContext="true" />

          </security>

        </binding>

      </wsHttpBinding>

    </bindings>

  </system.serviceModel>


스테이지 or 운영(라이브) https 일 경우 ( 빨간색 처리 부분이 핵심이다. )


 <system.serviceModel>

    

    <services>

      <service name="ABCService" behaviorConfiguration="ServiceBehavior">

          <endpoint address="rest" binding="webHttpBinding" contract="ABCService" bindingConfiguration="webHttpBinding" behaviorConfiguration="web" />

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

        <behavior name="ABCServiceBehavior">

          <serviceMetadata httpGetEnabled="true"/>

          <serviceDebug includeExceptionDetailInFaults="true"/>

        </behavior>

      </serviceBehaviors>

      <endpointBehaviors>

        <behavior name="web">

          <webHttp />

        </behavior>

      </endpointBehaviors>

    </behaviors>

    <bindings>

      <basicHttpBinding>

        <binding name="basicBinding" closeTimeout="00:01:00" openTimeout="00:01:00"

         receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"

         bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"

         maxBufferPoolSize="524288" maxBufferSize="65536" maxReceivedMessageSize="65536"

         textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"

         messageEncoding="Text">

          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />

          <security mode="Transport">

            <transport clientCredentialType="None" proxyCredentialType="None" realm="" />

            <message clientCredentialType="UserName" algorithmSuite="Default" />

          </security>

        </binding>

      </basicHttpBinding>

      <wsHttpBinding>

        <binding name="WSHttpBinding" closeTimeout="00:01:00"

         openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"

         bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"

         maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"

         textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">

          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"

           maxBytesPerRead="4096" maxNameTableCharCount="16384" />

          <reliableSession ordered="true" inactivityTimeout="00:10:00"

           enabled="false" />

          <security mode="Transport">

            <transport clientCredentialType="None" proxyCredentialType="None" realm="" />

            <message clientCredentialType="Windows" negotiateServiceCredential="true" establishSecurityContext="true" />

          </security>

        </binding>

      </wsHttpBinding>

      <webHttpBinding>

        <binding name="webHttpBinding">

          <security mode="Transport">

            <transport clientCredentialType="None" proxyCredentialType="None" realm="" />

          </security>

        </binding>

      </webHttpBinding>

    </bindings>

  </system.serviceModel>


이렇게 빨간색 처리를 해 주면 된다. https SSL 인증서 처리 때문에 생기는 부분 처리 작업.


추가로 둘다 사용하는 법


<services>
    <service name="MyNamespace.MyService" 
             behaviorConfiguration="MyServiceBehavior">
       <endpoint 
           address="basic" 
           binding="basicHttpBinding" 
           contract="MyNamespace.IMyService"/>
       <endpoint 
           address="secure" 
           binding="basicHttpBinding" bindingConfiguration="HttpsBinding"  
           contract="MyNamespace.IMyService"/>
    </service>
</services>


<bindings>
  <basicHttpBinding>
    <binding name="HttpBinding">
      <security mode="None">
        <transport clientCredentialType="None"></transport>
      </security>
    </binding>
    <binding name="HttpsBinding">
      <security mode="Transport">
          <transport clientCredentialType="Windows" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>


반응형