我有以下BaseApiController:从这个的WebAPI BaseController - 检查用户
public class BaseApiController : ApiController
{
public readonly Current _current { get; private set; }
}
我所有的ApiControllers
继承。
我需要在我的ApiControllers
中的所有方法中进行验证,该验证检查userId是否通过匹配当前的HttpContext.Current.User.Identity。
[Route("{userId}/cars")]
public HttpResponseMessage GetCars(int userId)
{
if (userId != _current.UserId)
{
return Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Unauthorized");
}
}
反正有这样做的BaseApiController,让我能避免对所有收到的userid参数端点这样做验证?
验证实际上应该做什么?你可以制作一个受保护的方法并调用它。属性可能很有趣,也可以看一下。 –
该验证用于检查管道上的令牌/声明是否与Api消费者发送的{userId}匹配。 – user3362714
我知道什么正在验证,但应执行什么操作?每次调用这个动作都一样吗? –