1
我有檢查認證和認證時使用的WebAPI返回錯誤的狀態代碼
throw new WebFaultException(HttpStatusCode.Unauthorized);
但是這仍然會返回一個404 Not Found狀態碼到客戶端/測試客戶端失敗,拋出異常的操作處理。
這是我的操作處理
public class AuthOperationHandler : HttpOperationHandler<HttpRequestMessage, HttpRequestMessage>
{
RequireAuthorizationAttribute _authorizeAttribute;
public AuthOperationHandler(RequireAuthorizationAttribute authorizeAttribute) : base("response")
{
_authorizeAttribute = authorizeAttribute;
}
protected override HttpRequestMessage OnHandle(HttpRequestMessage input)
{
IPrincipal user = Thread.CurrentPrincipal;
if (!user.Identity.IsAuthenticated)
throw new WebFaultException(HttpStatusCode.Unauthorized);
if (_authorizeAttribute.Roles == null)
return input;
var roles = _authorizeAttribute.Roles.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
if (roles.Any(role => user.IsInRole(role)))
return input;
throw new WebFaultException(HttpStatusCode.Unauthorized);
}
}
難道我做錯了什麼?
這並不好,我打算將我的api(基於預覽版6)遷移到明天的測試版。我有一個基於操作處理程序的驗證處理程序:( – 2012-02-20 19:48:52
@AntonyScott ActionFilters能夠完成大部分可以用OperationHandlers做的事情。 – 2012-02-20 20:39:39
嗯,好吧,看起來我明天有一點改寫:) – 2012-02-20 21:01:36