我有一個CustomAuthorizeAttribute
類以這種方式實現。使用自定義授權時,響應始終爲200確定
Public Overrides Sub OnAuthorization(actionContext As HttpActionContext)
If Authorize(actionContext) Then
Return
End If
HandleUnauthorizedRequest(actionContext)
End Sub
Protected Overrides Sub HandleUnauthorizedRequest(actionContext As HttpActionContext)
Dim challengeMessage = New HttpResponseMessage(HttpStatusCode.Unauthorized)
challengeMessage.Headers.Add("WWW-Authenticate", "Basic")
Throw New HttpResponseException(challengeMessage)
End Sub
Private Function Authorize(actionContext As HttpActionContext) As Boolean
Dim isAuthorized = False
Try
'make it true if all goes validations go well
Return True
Catch generatedExceptionName As Exception
End Try
Return isAuthorized
End Function
當授權失敗時,它會打在Throw New HttpResponseException(challengeMessage)
和預期不會進入服務端點。問題是我的HTTPResponse=200 OK
當我調用API而不是403 UnAuthorized
。我的代碼有什麼問題?
更新:
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method, AllowMultiple:=False, Inherited:=True)>
Public Class CustomAuthorizeAttribute
Inherits AuthorizeAttribute
,您在繼承哪個類和命名空間的'CustomAuthorizeAttribute'? –
@DavidTansey,更新了問題。 – naveen