Search code examples
c#web-serviceswcf

Web service auto-redirection to http or https


I have just updated a C# web service client from the old web-services, to the newer service, I believe it's called WCF? In the old webservices, there was a property for AllowAutoRedirect, to automatically redirect from http to https. It didn't work well, but there was also a Url property, so if the connection failed on http, I could write code to change the URL to https and try again.

In the new WCF method, is there a way to do this? Would I have to add a reference to a second service, with the same address, but https?

(I have turned SSL on my website on and off a few times in the past, and it's necessary for my software out in the field to automatically handle this, and use whichever is available).


Solution

  • You can do this by adding rewrite rules. This will rewrite any incoming request from HTTP to HTTPS. You can check these posts below to find the solution.

    <rule name="Redirect to HTTPS" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
    </rule>
    

    IIS redirect/rewrite .asmx service
    wcf webservice and URL forwarding with https
    HttpClient doesn't redirect even when AllowAutoRedirect = true
    Can i use a 301 redirect to allow an existing .Net Web service to keep functioning without requiring client updates?