WCF Rest Web Service Request Error when using form

2019-09-05 03:25发布

I receive the following request error from my WCF Rest web service when using forms Authentication and Authorization. It works fine without the Authentication and Authorization:-

"The server encountered an error processing the request. Please see the service help page for constructing valid requests to the service."

Its built in .net 4 so no .svc file, here is the service code:--

        namespace WcfRestService1
{

    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]

    public class Service1
    {


        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        public string GetHelloMessage()
        {
            return ("hello from web service");

        }

    }
}

Here is the web config code:--

 <configuration>




  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>



  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
        <standardEndpoint name="" helpEnabled="true" crossDomainScriptAccessEnabled="true" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>



    <system.web>
            <authentication mode="Forms">
                <forms defaultUrl="Service1" timeout="20"
                     ticketCompatibilityMode="Framework40"
                     loginUrl="login.aspx" name=".Mobile-Rest-Api" cookieless="UseCookies"/>
            </authentication>


        <authorization>
            <deny users="?" />

            <allow users="*"/>

        </authorization>
        <!--<authentication mode="None"/>-->
        </system.web>



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


</configuration>

Any help on this would be great, thanks in advance.

1条回答
戒情不戒烟
2楼-- · 2019-09-05 04:26

The problem is that you are using a authentication method that does not support a services call.

Forms authentication is used when a user is accessing a site, if the user is not authenticated he is directed to a login form, in which he fills out user name and password.

When a service is making a call, the service gets a redirect response, which it is not able to handle, therefore the error.

You need to select a different authentication method.

查看更多
登录 后发表回答