1

I was able to implement bearer token based authentication in WebService (built using WebApi 2 with OData v3) which lies in the same MVC4 project ( Cookies based authentication is implemented for MVC4 project). But then when hosted in server I got the problem to issue token.

Response from localhost using PostMan enter image description here

Response from server

enter image description here

Part of code from 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;





    }
}

Edited: Raw response using Postman

 <!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>

Edited: Part of code from Web.config file

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

I will highly appreciate if any sample or guide provided. Thank you.

Ishwor Khanal
  • 1,312
  • 18
  • 30
  • What does the raw response show you? – Jasen Feb 09 '18 at 23:51
  • @Jasen Please check the post I have edited as per your request. Thank you. – Ishwor Khanal Feb 09 '18 at 23:55
  • You need to have proper exception handling and logging to figure out what is going wrong there... Does the application have any dependency on database or other service which might available on the servrr? – Chetan Feb 09 '18 at 23:57
  • So it's an internal server error. You'll need enable error details to get more information about what went wrong. – Jasen Feb 09 '18 at 23:58
  • @Jasen, Shall I have to configure in WebApiConfgi.cs to get error detail? What else outcome will be if I set IncludeErrorDetailPolicy.Default instead LocalOnly? – Ishwor Khanal Feb 10 '18 at 00:18
  • It's usually a IIS setting in your web.config. – Jasen Feb 10 '18 at 00:21
  • @Jasen, Please have a look the post again, I have updated the part of Web.config code other than this I do have only connection configuration and installed packages in Web.config file. I have not modified anything in Web.config file because MVC4 project is using cookie based authentication. Thank you for your time though. – Ishwor Khanal Feb 10 '18 at 00:29
  • Try this https://stackoverflow.com/questions/11665322/how-to-set-web-config-file-to-show-full-error-message. The details are hidden by default so you don't leak sensitive info but it makes debugging difficult. Get the details so you can properly investigate your error. – Jasen Feb 10 '18 at 00:33
  • @Jasen I got the root cause as Mime type was not set in the server. I wonder how can I set Mime type for all types of files as server has two fields (FileType and Extension) to add the Mime type in server? Thank you. – Ishwor Khanal Feb 10 '18 at 01:27

0 Answers0