开发一组 WCF REST API 来与 Flutter 应用程序交互。
调用 GET 请求仅适用于文件,但在尝试使用 POST 调用时我遇到此错误:
{"Message":"Authentication failed","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}
这是请求配置:
<OperationContract()>
<WebInvoke(BodyStyle:=WebMessageBodyStyle.Bare, Method:="POST", RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json, UriTemplate:="getPopUpdata")>
Function getPopUpdata(data As PopUpStatoDati) As GetResult(Of List(Of GenericCodeValue(Of String)))
当我在
aspNetCompatibilityEnabled
中启用 web.config
以便使 HttpContext
通过分段请求将文件上传到服务器时,此问题开始出现。
此外,在我的机器上开发 API 时,我没有遇到这个问题,可能是因为我没有使用
https
也不是 IIS
,而是使用 IISExpress
我已经尝试按照前面问题中的建议通过 IIS 配置启用匿名身份验证,并将其添加到 Web 配置中:
<authorization>
<allow users="*" />
</authorization>
在其他答案中,我还发现在 /App_Start/RouteConfig.cs 中设置
AutoRedirectMode = RedirectMode.Off
可能会解决问题,但我的项目中没有这样的文件。
目前我的
web.config
文件看起来像这样
<configuration>
<appSettings>
<add key="aspnet:MaxJsonDeserializerMembers" value="6000" />
<add key="aspnet:MaxHttpCollectionKeys" value="6000" />
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="false" />
<add key="wcf:serviceHostingEnvironment:useClassicReadEntityBodyMode" value="true" />
</appSettings>
<system.web>
<globalization culture="it-IT" uiCulture="it-IT" />
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
<httpRuntime />
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
<customErrors mode="Off" />
<authorization>
<allow users="*" />
</authorization>
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="secureHttpBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" crossDomainScriptAccessEnabled="true" maxBufferSize="65536" transferMode="Streamed">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="WebService.App" behaviorConfiguration="WebService.appBehavior">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="secureHttpBinding" behaviorConfiguration="WebService.appBehavior" contract="WebService.IApp">
</endpoint>
<endpoint contract="IMetadataExchange" binding="mexHttpsBinding" address="mex" />
<host>
<baseAddresses>
<add baseAddress="***/services/App" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WebService.appBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="WebService.appBehavior">
<serviceMetadata httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="4294967295" maxUrl="4294967295" maxQueryString="4294967295" />
</requestFiltering>
</security>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
一些深入了解的建议:
<customErrors mode="On" />
以检索额外信息。希望这能让您了解问题所在。