全新的ServiceStack,请原谅我,如果这是一个简单的。
我正在编写一个API,它将使用自定义HTTP标头来获取身份验证信息。我添加了一个RequestFilter,如下所示:
RequestFilters.Add((httpReq, httpResp, requestDto) =>
{
if(httpReq.Headers["MyMagicHeader"] != "magic")
{
throw HttpError.Unauthorized("Unauthorized");
}
else
{
//TODO: Populate a "Client" object accessible by the Service
}
});
我的问题是,我现在如何根据魔术标题中的值为我创建的“客户”对象提供相关服务?
从它的外观来看,我唯一的选择就是通过DTO传递这些信息。所以我考虑添加一个我所有DTO继承的基类,这个基类将包含一个Client属性。
这是正确的方法吗?
答案 0 :(得分:3)
在同一请求中传递所有过滤器和服务中可用信息的方法是使用httpReq.Items
对象字典,例如Dictionary<string,object>
“
RequestFilters.Add((httpReq, httpResp, requestDto) =>
{
if(httpReq.Headers["MyMagicHeader"] != "magic")
{
throw HttpError.Unauthorized("Unauthorized");
}
else
{
//TODO: Populate a "Client" object accessible by the Service
httpReq.Items["MagicToken"] = CreateMagicValue(httpReq.Headers["MyMagicHeader"]);
}
});