title: Unauthorised webapi call returning login page rather than 401
body: <p>How do I configure my mvc/webapi project so that a webapi method called from a razor view doesn't return the loginpage when its unauthorised?</p>
  
  <p>Its a MVC5 application which also has WebApi controllers for calls via javascript.</p>
  
  <p>The two methods below</p>
  
  <pre><code>[Route("api/home/LatestProblems")]      
  [HttpGet()]
  public List&lt;vmLatestProblems&gt; LatestProblems()
  {
      // Something here
  }
  
  [Route("api/home/myLatestProblems")]
  [HttpGet()]
  [Authorize(Roles = "Member")]
  public List&lt;vmLatestProblems&gt; mylatestproblems()
  {
     // Something there
  }
  </code></pre>
  
  <p>are called via the following angular code:</p>
  
  <pre><code>angular.module('appWorship').controller('latest', 
      ['$scope', '$http', function ($scope,$http) {         
          var urlBase = baseurl + '/api/home/LatestProblems';
          $http.get(urlBase).success(function (data) {
              $scope.data = data;
          }).error(function (data) {
              console.log(data);
          });
          $http.get(baseurl + '/api/home/mylatestproblems')
            .success(function (data) {
              $scope.data2 = data;
          }).error(function (data) {
              console.log(data);
          });  
      }]
  );
  </code></pre>
  
  <p>So I'm not logged in and the first method successfully returns data. the second method returns (in the success function) data which contains the equivalent of a login page. i.e. what you would get in mvc if you requested a controller action which was stamped with [Authorize] and you weren't logged in.</p>
  
  <p>I want it to return a 401 unauthorized, so that i can display different data for users based on if they are logged in or not.  Ideally if the user is logged in i want to be able to access the Controller's User property so i can return data specific to that Member.</p>
  
  <p><strong>UPDATE: Since none of the suggestions below seem to work anymore (changes to Identity or WebAPI) ive created a raw example on <a href="https://github.com/tbertenshaw/MVCAPI" rel="noreferrer">github</a> which should illustrate the problem.</strong></p>