title: 401 Unauthorized when making request to Azure Mobile Service
body: <p>I have an Azure Mobile service which I am using for authentication. I have a custom auth provider which, once validated, returns the following information:</p>
  
  <pre><code>JwtSecurityToken token = AppServiceLoginHandler.CreateToken(claims, signingKey, audience, issuer, null);
  return Ok(new LoginResult()
  {
    AuthenticationToken = token.RawData,
    User = signinedInUser,
    StatusCode = System.Net.HttpStatusCode.OK
  });
  </code></pre>
  
  <p>Notice the <code>Timespan</code> is set to <code>null</code> so the token doesn’t expire.</p>
  
  <p>I then make a subsequent request to my AMS which has a controller protected with the <code>Authorize()</code> attribute. However, these are all failing with a <code>401 Unauthorized</code> response before any of my breakpoints are being hit.</p>
  
  <p>I can see from the Azure logs where this is happening:</p>
  
  <blockquote>
    <p>2017-10-05T12:18:54  PID[5524] Information Request, Method=POST,
    Url=<a href="https://mywebsite.azurewebsites.net/api/userinfo/update" rel="nofollow noreferrer">https://mywebsite.azurewebsites.net/api/userinfo/update</a>,
    Message='<a href="https://mywebsite.azurewebsites.net/api/userinfo/update" rel="nofollow noreferrer">https://mywebsite.azurewebsites.net/api/userinfo/update</a>'</p>
    
    <p>2017-10-05T12:18:54  PID[5524] Information Message='UserInfo',
    Operation=DefaultHttpControllerSelector.SelectController</p>
    
    <p>2017-10-05T12:18:54  PID[5524] Information
    Message='MyAMS.Controllers.UserInfoController',
    Operation=DefaultHttpControllerActivator.Create</p>
    
    <p>2017-10-05T12:18:54  PID[5524] Information
    Message='MyAMS.Controllers.UserInfoController',
    Operation=HttpControllerDescriptor.CreateController</p>
    
    <p>2017-10-05T12:18:54  PID[5524] Information Message='Selected action
    'Update(User cpUser)'',
    Operation=ApiControllerActionSelector.SelectAction</p>
    
    <p>2017-10-05T12:18:54  PID[5524] Information Message='Will use same
    'JsonMediaTypeFormatter' formatter',
    Operation=JsonMediaTypeFormatter.GetPerRequestFormatterInstance</p>
    
    <p>2017-10-05T12:18:54  PID[5524] Information Message='Selected
    formatter='JsonMediaTypeFormatter', content-type='application/json;
    charset=utf-8'', Operation=DefaultContentNegotiator.Negotiate</p>
    
    <p>2017-10-05T12:18:54  PID[5524] Information
    Operation=AuthorizeAttribute.OnAuthorizationAsync, Status=401
    (Unauthorized)</p>
    
    <p>2017-10-05T12:18:54  PID[5524] Information
    Operation=UserInfoController.ExecuteAsync, Status=401 (Unauthorized)</p>
    
    <p>2017-10-05T12:18:54  PID[5524] Information Response, Status=401
    (Unauthorized), Method=POST,
    Url=<a href="https://mywebsite.azurewebsites.net/api/userinfo/update" rel="nofollow noreferrer">https://mywebsite.azurewebsites.net/api/userinfo/update</a>,
    Message='Content-type='application/json; charset=utf-8',
    content-length=unknown'</p>
  </blockquote>
  
  <p>You can see that the Authorize attribute is setting a 401 response:</p>
  
  <blockquote>
    <p>2017-10-05T12:18:54  PID[5524] Information
    Operation=AuthorizeAttribute.OnAuthorizationAsync, Status=401
    (Unauthorized)</p>
  </blockquote>
  
  <p>On the client, I an populating both the User ID and the Auth Token:</p>
  
  <pre><code>this.client = new MobileServiceClient("https://mywebsite.azurewebsites.net");
  var user = UserProfileService.GetCurrentSignedInUser();
  client.CurrentUser = new MobileServiceUser(user.UserId.ToString())
  {
      MobileServiceAuthenticationToken = user.AuthToken
  };
  </code></pre>
  
  <p>And stepping through the code I have confirmed that the UserID matches that of the user and also the AuthToken is the same AutToken returned in my login method.</p>
  
  <p>Is there something else I need to set/do to enabled authenticated requests to an Azure Mobile Service?</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT</strong>
  I have since disabled all other authentication providers in Azure but this hasn't solved the problem.
  I have also debugged the code locally and the issue does not occur running on my localhost, only when deployed to Azure.</p>