WebApi 2基于承载的令牌可从localhost访问,但不能从服务器访问

时间:2018-02-09 23:42:49

标签: c# asp.net-mvc-4 asp.net-web-api bearer-token

我能够在WebService(使用带有OData v3的WebApi 2构建)中实现基于承载令牌的身份验证,该身份验证位于同一个MVC4项目中(基于Cookie的身份验证是针对MVC4项目实现的)。但是当托管在服务器中时,我遇到问题就是发出令牌。

使用PostMan从localhost响应 enter image description here

来自服务器的响应

enter image description here

来自 WebApiConfig.cs

的部分代码
 public static class WebApiConfig
 {
    public static void Register(HttpConfiguration config)
    {

        /*

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
         * */

        config.Filters.Add(new ValidateModelAttribute());
        config.Filters.Add(new AuthorizeAttribute());            

        var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
        jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();


        config.EnsureInitialized();
        config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.LocalOnly;
        config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;





    }
}

编辑: 使用邮递员的原始回复

 <!DOCTYPE html>
<html>
<head>
    <title>Internal Server Error</title>
    <style type="text/css">
        body {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            line-height: 20px;
            color: #444;
        }
        .page {
            width: 740px;
            margin: 50px auto;
            text-align: center;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Arial', 'Segoe UI', Arial, helvetica, sans-serif;
            font-weight: 400;
            margin: 0;
        }
        h1 {
            font-size: 100px;
            line-height: 140px;
            color: #999;
            margin-bottom: 50px;
        }
        p {
            font-size: 16px;
            line-height: 22px;
        }
    </style>
</head>
<body>
    <div class="page">

        <img src="Content/Images/sad.png" width="128" height="128" />

        <h1>
            Oops!
        </h1> 

        <p>
            We apologize, an error occurred while handling your request, this is not a problem with your computer or internet connection.
            The details have been sent to our support team and we will investigate the issue very soon.
            <br />
            <br />
            In the meantime, please retry your request as it may have been temporary.
        </p>

    </div>
</body>
</html>

编辑: 来自 Web.config 文件

的部分代码
     <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime requestValidationMode="2.0" targetFramework="4.5" maxRequestLength="1048576" />
    <authentication mode="Forms">
      <forms loginUrl="~/Default" timeout="2880" />
    </authentication>
    <sessionState timeout="60"></sessionState>
    <pages validateRequest="false">
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
    <customErrors mode="RemoteOnly" defaultRedirect="~/404.html">
      <error redirect="~/404.html" statusCode="404" />
      <error redirect="~/500.html" statusCode="500" />
    </customErrors>
    <!--<customErrors mode="Off">    
    </customErrors>-->
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="4294967295" />
      </requestFiltering>
    </security>
    <!--<modules>
      <remove name="RoleManager" />
    </modules>-->
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>

如果提供任何样本或指南,我将非常感谢。 谢谢。

0 个答案:

没有答案