我正在使用asp.net WebAPI,我需要創建一個自定義ActionFilter,它可以快速檢查請求URI的用戶是否應該實際上能夠獲取數據。返回狀態代碼未經授權在WebAPI中定製IActionFilter
他們已被授權通過基本身份驗證使用Web服務,並且他們的角色已通過自定義角色提供者進行驗證。
我需要做的最後一件事是檢查他們是否有權查看他們請求的數據並使用其URI中的參數。
這裏是我的代碼:
public class AccessActionFilter : FilterAttribute, IActionFilter
{
public System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, System.Threading.CancellationToken cancellationToken, Func<System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>> continuation)
{
var result = //code to see if they have permission returns either 0 or 1
if (result==0) {
throw new ArgumentException("You do not have access to this resource");
}
return continuation();
}
}
目前,我只是把它不是我想要的,我寧願回到System.Net.HttpStatusCode.Unauthorized
一個錯誤,但我對我重寫方法有點惱火,我做不完全理解它。
我該如何去解決這個問題?
由於授權不只是失蹤,而是身份已經提供,但用戶不能訪問,適當的HTTP響應是403禁止的。 –