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