我创建了一个自定义的 AuthorizeAttribute
,用于验证用户的一些 OAuth凭证
。
一旦我获得了有效的用户,我想在web api .net中将响应数据返回给控制器,我该如何实现这一点。
public class CustomAttribute : AuthorizeAttribute
{
protected override bool IsAuthorized(HttpActionContext actionContext)
{
var response = mydata.Result.Content.ReadAsStringAsync();
if (mydata.Result.StatusCode == HttpStatusCode.OK)
{
// return response data to controller
return true;
}
}
}
我搜索了一下,发现在MVC中可以像下面这样做:
我搜索到in mvc
,可以像下面这样做。
public class CustomAttribute : AuthorizeAttribute
{
public string BlackListedUsers { get; set; }
protected override bool AuthorizeCore(AuthorizationContext filterContext)
{
filterContext.HttpContext.Items["test"] = "foo";
return true;
}
}
在控制器中 -
_yourVariable = HttpContext.Items["test"];
在web api
中,我该如何使用System.Web.Http
实现这个功能,因为在webapi中我没有方法AuthorizeCore和输入参数AuthorizationContext
?