- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于基本身份验证,我已经实现了自定义 HttpMessageHandler
基于 Darin Dimitrov 的答案中显示的示例:https://stackoverflow.com/a/11536349/270591
代码创建一个实例principal
类型 GenericPrincipal
包含用户名和角色,然后将此主体设置为线程的当前主体:
Thread.CurrentPrincipal = principal;
稍后在 ApiController
方法主体可以通过访问 Controller User
来读取属性:
public class ValuesController : ApiController
{
public void Post(TestModel model)
{
var user = User; // this should be the principal set in the handler
//...
}
}
这似乎工作正常,直到我最近添加了一个自定义 MediaTypeFormatter
使用Task
像这样的库:
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream,
HttpContent content, IFormatterLogger formatterLogger)
{
var task = Task.Factory.StartNew(() =>
{
// some formatting happens and finally a TestModel is returned,
// simulated here by just an empty model
return (object)new TestModel();
});
return task;
}
(我有这种方法从一些示例代码中使用 Task.Factory.StartNew
in ReadFromStreamAsync
启动任务。这是错误的,也许是问题的唯一原因?)
现在,“有时” - 对我来说它似乎是随机的 - User
Controller 方法中的主体不再是我在 MessageHandler 中设置的主体,即用户名 Authenticated
旗帜和角色全部丢失。原因似乎是自定义 MediaTypeFormatter 导致 MessageHandler 和 Controller 方法之间的线程发生变化。我通过比较 Thread.CurrentThread.ManagedThreadId
的值证实了这一点在 MessageHandler 和 Controller 方法中。 “有时”它们是不同的,然后校长就“迷失”了。
我现在正在寻找设置 Thread.CurrentPrincipal
的替代方案以某种方式将主体安全地从自定义 MessageHandler 传输到 Controller 方法并在 this blog post 中使用请求属性:
request.Properties.Add(HttpPropertyKeys.UserPrincipalKey,
new GenericPrincipal(identity, new string[0]));
我想测试一下,但似乎HttpPropertyKeys
类(位于命名空间 System.Web.Http.Hosting
中)没有 UserPrincipalKey
最近的 WebApi 版本(候选版本和上周的最终版本)中不再包含此属性。
我的问题是:如何更改上面的最后一个代码片段,以便它适用于当前的 WebAPI 版本?或者一般来说:如何在自定义 MessageHandler 中设置用户主体并在 Controller 方法中可靠地访问它?
编辑
提到here “HttpPropertyKeys.UserPrincipalKey
... 解析为 “MS_UserPrincipal”
”,所以我尝试使用:
request.Properties.Add("MS_UserPrincipal",
new GenericPrincipal(identity, new string[0]));
但它并没有像我预期的那样工作:ApiController.User
属性不包含添加到 Properties
的主体以上集合。
最佳答案
这里提到了在新线程上丢失主体的问题:
http://leastprivilege.com/2012/06/25/important-setting-the-client-principal-in-asp-net-web-api/
Important: Setting the Client Principal in ASP.NET Web API
Due to some unfortunate mechanisms buried deep in ASP.NET, setting Thread.CurrentPrincipal in Web API web hosting is not enough.
When hosting in ASP.NET, Thread.CurrentPrincipal might get overridden with HttpContext.Current.User when creating new threads. This means you have to set the principal on both the thread and the HTTP context.
这里:http://aspnetwebstack.codeplex.com/workitem/264
Today, you will need to set both of the following for user principal if you use a custom message handler to perform authentication in the web hosted scenario.
IPrincipal principal = new GenericPrincipal(
new GenericIdentity("myuser"), new string[] { "myrole" });
Thread.CurrentPrincipal = principal;
HttpContext.Current.User = principal;
我已将最后一行 HttpContext.Current.User =principal
(需要 using System.Web;
)添加到消息处理程序和 User
ApiController
中的 code> 属性现在始终具有正确的主体,即使线程由于 MediaTypeFormatter 中的任务而发生更改也是如此。
编辑
强调一下:仅当 WebApi 托管在 ASP.NET/IIS 中时,才需要设置 HttpContext
当前用户的主体。对于自托管来说,没有必要(也不可能,因为 HttpContext
是一个 ASP.NET 构造,并且在自托管时不存在)。
关于asp.net - 如何在自定义 WebAPI HttpMessageHandler 中安全地设置用户主体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12028604/
在我的 java 代码中,我做了类似的事情: int sleep = 0; sleep(sleep); sleep++; 被bos指出不好。它不能在 php 中正常工作。 在 java 中使用上述代码
我有一个程序使用第 3 方库进行一些计算,我在与主应用程序不同的线程上启动该程序。不幸的是,此计算可能需要很长时间,并且不提供进度更新和取消的接口(interface)。 为了拥有这样的界面,我想创建
C++ 是否有任何等效于 python 的函数 os.path.join?基本上,我正在寻找将文件路径的两个(或多个)部分组合在一起的东西,这样您就不必担心确保这两个部分完美地结合在一起。如果它在 Q
我正试图站起来(非商业)web application使用 neo4j Community 3.5.2 作为后端。 应用程序以两种方式与图形交互: 服务器端(安全的)用 flask 编写新的节点和关系
我正在开发一个将有许多外部用户的在线应用程序。至于现在,我的连接方法是为所有用户托管一个中央数据库,而他们从自己的服务器文件连接。 方法: PHP 连接文件(托管在他们的服务器上;文件由我提供) >>
我创建了一个将所有事件通知代码转换为字符串的函数。真的很简单。 我有一堆常量,比如 const _bstr_t DIRECTSHOW_MSG_EC_ACTIVATE("A video window i
我想将(附加)信息从过滤器传递到资源。我目前尝试这样做的方式是,在 Filter 中: getContext().getAttributes().put("additionalInformation"
我想计算转换系数。为此,我必须除以例如的最大值。 ushort 为 uchar 的最大值。 我想通过将参数传递给函数或类型名来动态地执行此操作。然后我想选择最大值并执行计算。 有两个问题: 如何动态选
我希望我的用户在用 Java 请求列表时能够编写自己的过滤器。 选项 1) 我正在考虑将 JavaScript 与 Rhino 结合使用。 我将用户的过滤器作为 javascript 字符串获取。然后
(安全地)提供来自不同域的图像是否符合 PCI 标准?我搜索了 PCI DSS 2.0 PDF,但没有找到任何引用资料。 最佳答案 图像不符合 PCI 合规性。 PCI DSS covers the
我们正在将 spring 和 hibernate 用于 web 应用程序:该应用程序有一个购物车,用户可以在其中放置商品。为了保存不同登录名之间要查看的项目,购物车中的项目值存储在表中。提交购物车时,
我正在为多个客户创建一个具有电子商务元素的 Rails 应用程序 - 我希望这些客户能够在管理区域中指定计算运费的公式;因为方法可能不同。 让我们假装一下,我允许他们输入 ruby 代码,然后我稍
我正在 Eclipse 中开发一个 Java 项目,使用 Maven 构建和管理依赖项。该项目分布在 5 个 Eclipse 项目中,其中一个是父 POM。我正在研究基于另一个团队实现的更复杂服务器的
我想在 ADO.NET 数据服务中存储每线程数据。在线程特定的静态变量上使用 ThreadStatic 属性是否安全,或者我会遇到问题吗?我担心的是,我的 ThreadStatic 变量在请求完成并且
Stackoverflow 上至少有一篇与此主题相关的帖子:Generate password in python 你会发现这个主题甚至在 PEP 中也受到了一些批评。这里提到:https://www
对于我工作中的一个项目,我需要创建一个独立的 Python 安装(来自源代码)。然而,完整的目录占用大约 90MB 的磁盘空间,虽然不多,但太多了,无法一遍又一遍地复制。 我可以从自定义 python
例如,我有一张学生表,我有一本 Python 字典 mydict = {"fname" : "samwise", "lname" : "gamgee", "age" : 13} 我怎样才能安全地生成一
我经常在代码中遇到使用 memset 手动零初始化的 POD 结构,如下所示: struct foo; memset(&foo, 0, sizeof(foo)); 我检查了 C++11 标准,它说:“
我是一名优秀的程序员,十分优秀!