发布后发布后返回400错误请求

时间:2018-07-11 14:01:18

标签: c# wcf asp.net-web-api jwt bad-request

看到我的POST方法返回400 Bad Request之后,我完全被困住了。奇怪的是,他们在Local上工作得很好。当我发布API项目并将其部署到我们的测试服务器时,POST方法将停止工作。

这是我的web.config文件:

<configuration>                                               
    <configSections>
        <section name="entityFramework" 
                 type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral,  PublicKeyToken=b77a5c561934e089" requirePermission="false" />    
    </configSections>                                                
    <appSettings></appSettings>                                       
    <system.web>
        <compilation debug="true" targetFramework="4.6.1" />
        <httpRuntime targetFramework="4.6.1" />                          
    </system.web>                                               
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
            <remove name="WebDAVModule" /> <!-- In order to fix 405 Error on Put method -->
        </modules>
        <handlers>
            <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
            <remove name="OPTIONSVerbHandler" />
            <remove name="TRACEVerbHandler" />
            <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." 
                 verb="GET,HEAD,POST,DEBUG,PUT,DELETE"  
                 type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>
        <!--<security>
            <requestFiltering>
                <verbs>
                    <add verb="GET" allowed="true" />
                    <add verb="POST" allowed="true" />
                    <add verb="PUT" allowed="true" />
                    <add verb="DELETE" allowed="true" />
                </verbs>
            </requestFiltering>
        </security>-->                                             
    </system.webServer>                                                  
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Newtonsoft.Json"  
                                  publicKeyToken="30ad4fe6b2a6aeed" />
                <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
            </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" />
  </dependentAssembly>
</assemblyBinding>                                                  
</runtime>                                                    
<system.codedom>
<compilers>
  <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
  <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
</compilers>                                                 
</system.codedom>                                            
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="mssqllocaldb" />
  </parameters>
</defaultConnectionFactory>
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="XDataContext" connectionString="data source=XXX.XXX.XX.XX;initial catalog=X;user id=X;password=X;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />                    
</connectionStrings>                                          
</configuration>

我已在此处IIS hosted WCF Service return HTTP 400 Bad Request和此处ASP.NET Web API "400 Bad Request" on POST Request When Deploying to Test Server尝试了建议的解决方案,但不幸的是,在我的情况下,这些解决方案都不起作用。

最后一点是,当我收到此错误时,Fiddler上的错误消息也完全为空。

{"Message":""}

谢谢!

编辑:

我将安全标签注释掉,因为它们也不能解决我的问题。

编辑2:

下面是在服务器上返回错误请求,但在本地上可以正常工作的方法:

public HttpResponseMessage PostLogin(LoginWM loginWM)
    {
        try
        {
            if (_userTokenManager.CheckUser(loginWM))
            {
                _userTokenManager.SaveToken(loginWM.Email, loginWM.Password);
                return Request.CreateResponse(HttpStatusCode.Created, _userTokenManager.PublishToken(loginWM.Email, loginWM.Password));
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, new Exception("User not found."));
            }
        }
        catch (Exception ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
        }
    }

我要发送的内容(JSON格式)如下:

{"Email":"ozgur@gmail.com","Password":"oa123"}

这是我用于上述JSON的类:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XX.XX.Model.WebModel
{
    public class LoginWM
    {
    [Required]
    [StringLength(50)]
    public string Email { get; set; }

    [Required]
    [StringLength(20)]
    public string Password { get; set; }
}

0 个答案:

没有答案