如何从REST响应中返回401

时间:2015-11-23 12:51:23

标签: asp.net rest

我有一个REST API,我想为其创建一个AccessToken,但是如果用户没有提供正确的凭据,我希望能够返回401,但我似乎无法计算/得到401返回而不是AccessToken

所以这是我的代码的基础知识(使用vb.net)

<WebInvoke(method:="POST", UriTemplate:="auth/token/",BodyStyle:=WebMessageBodyStyle.Bare, RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json)>
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

我想知道这个问题是因为我已经声明我的返回类型是_AccessToken我必须返回该类型,但我也认为必须有一种方法来覆盖它,但不知道如何。

0 个答案:

没有答案