title: How to return a 401 from a REST Response
body: <p>I have a REST API that I want to create an AccessToken for, but I want to be able to return a 401 if the user does not provide the correct credentials, but I can't seem to figure/get the 401 to return instead of the AccessToken </p>
  
  <p>So this is the basics of my code (using vb.net)</p>
  
  <pre><code>&lt;WebInvoke(method:="POST", UriTemplate:="auth/token/",BodyStyle:=WebMessageBodyStyle.Bare, RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json)&gt;
  Public Function Token() As _AccessToken
  
      Dim myAccessToken As New _AccessToken  'class to return to the user
      Dim Authenticated As Boolean = False
      Dim objRequest As HttpRequest = HttpContext.Current.Request
      Dim Username As String = objRequest.Headers("username")
      Dim Password As String = objRequest.Headers("password")
  
  
      Authenticated = AuthenticateUser(Username, Password)
  
      If (Authenticated = True) Then
  
          'Set the access token
          myAccessToken.ID = "sadfsdv32234fdgdg"
  
       Else
  
           HttpContext.Current.Response.StatusCode = 401
           HttpContext.Current.Response.SuppressContent = True
           HttpContext.Current.ApplicationInstance.CompleteRequest()
       End If
  
  
       Return myAccessToken
  
  End Function
  </code></pre>
  
  <p>I'm figuring the issue is because I have declared my return type to be _AccessToken I have to return that type, but I also figure there must be a way to override this, but not sure how.</p>