本文介绍了使用表单身份验证和授权时WCF Rest Web服务请求错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用表单身份验证和授权时,我从WCF Rest Web服务收到以下请求错误.没有身份验证和授权,它就可以正常工作:-

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."

它内置于.net 4中,因此没有.svc文件,这是服务代码:-

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");

        }

    }
}

这是Web配置代码:-

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.

推荐答案

问题是您使用的身份验证方法不支持服务调用.

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.

这篇关于使用表单身份验证和授权时WCF Rest Web服务请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:07
查看更多