Authentication mode=“Forms” causing errors in WCF

2019-07-18 05:52发布

问题:

I have a WCF end point inside my .NET 4.0 Web Application project. Using the VS2010 WCF Test Client, I can connect to the service correctly. However when I go to use the service I get a generic error message:

The content type text/html; charset=UTF-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 1024 bytes of the response were:

I have discovered that when I remove the authentication from the web.config file the service works correctly:

<authentication mode="Forms">
  <forms cookieless="AutoDetect" loginUrl="~/Security/LoginClient.aspx" name="FORMAUTH" />
</authentication>

Any ideas how I can remove just this service from the authentication?

回答1:

You can exclude specific files from forms authentication with a location node in web.config:

<location path="MyService.svc">
   <system.web>
      <authorization>
          <allow users="*" />
      </authorization>
   </system.web>
</location>

An easier way would just be to group the service related files don't require authentication into a public folder and allow access to the entire folder:

<location path="MyServiceFolder/">
   <system.web>
      <authorization>
         <allow users="*" />
      </authorization>
   </system.web>
</location>

More information on the location element on MSDN:

http://msdn.microsoft.com/en-us/library/b6x6shw7%28vs.71%29.aspx