我有一个 AuthorizeAttribute 类来拦截对我的 Web Api 的调用。在那里,我从给定的会话中验证用户。
如果用户拥有正确的凭据,我想在请求正文中附加在凭据检查期间获取的 userId。我尝试了一些东西,但似乎我无法访问 IsAuthorized 中的请求正文?
我正在尝试做这样的事情:
public class AuthorizeUserAttribute : AuthorizeAttribute
{
protected override bool IsAuthorized(HttpActionContext httpContext)
{
// Pick up session
var sessionKey = httpContext.Request.Headers.FirstOrDefault(h => h.Key == "session").Value;
// If session vas valid, get userid from session and add it to the request body somehow
// so the controller gets userid attached.
return true;
}
}
之后,目标控制器被调用:
public Response GetCandy( CandyRequest candy )
{
// Wohoo, the user was accepted and i've got the user id already in the argument object.
var userId = candy.UserId;
}