我需要为我的Web API调用添加安全性。由于我需要自定义验证,我决定创建一个自定义AuthorizeAttribute(我需要能够检查当前的主体)。
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class CustomWebAuthorize : AuthorizeAttribute
{
protected override bool IsAuthorized(System.Web.Http.Controllers.HttpActionContext actionContext)
{
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK);
using (var mgr = AuthenticationManager.CreateNew())
{
if (!mgr.IsAllowedAccess(actionContext.RequestContext.Principal))
{
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden);
}
}
var isAuthorized = actionContext.Response.StatusCode == HttpStatusCode.OK;
return isAuthorized;
}
}
我有一个如下定义的Controller动作:
[HttpGet]
[CustomWebAuthorize]
public IHttpActionResult TestPrdAccess()
{
return Ok(true);
}
如果我访问关联的url,调试器会破坏自定义属性代码(并且无错误地通过代码),但无论是true还是false,它都不会进入Controller方法。我也没有在浏览器上看到任何内容。
任何帮助?