【问题标题】:Change the context of a thread to other user将线程的上下文更改为其他用户
【发布时间】:2014-08-02 12:46:41
【问题描述】:

用户触发了一个事件,因此线程在该用户的上下文中。 我遍历所有连接的用户并希望向他们发送此事件,但我想使用经典授权来确定他们是否应该接收该事件。问题是线程主体等在触发事件的用户的上下文中。

我正在研究模拟线程,然后进行授权。我在上面找到了这篇文章

http://msdn.microsoft.com/en-us/library/ff647248.aspx#ImpersonationDelegation5

using System.Security.Principal;
…
WindowsIdentity wi = new WindowsIdentity(userName@fullyqualifieddomainName);
WindowsImpersonationContext ctx = null;

try
{
  ctx = wi.Impersonate();
  // Thread is now impersonating you can call the backend operations here...

catch
{
  // Prevent exceptions propagating.
}
finally
{
  // Ensure impersonation is reverted
  ctx.Undo();
}

ctx = wi.Impersonate();之后线程还在调用用户的上下文中,我做错了什么?

更新 我想以其他用户身份运行此代码

provider = AuthorizationFactory.GetAuthorizationProvider();
provider.Authorize(Thread.CurrentPrincipal, Rule)

我写的一篇小博文涵盖了所需的步骤 http://andersmalmgren.com/2014/06/12/client-server-event-aggregation-with-role-authorization/

【问题讨论】:

  • 是否出现异常?该代码没有捕获任何内容,所以我很好奇!你在 wcf 上下文中吗(因为你提到的文档是这样认为的) - 如果是这样,你会添加一个适当的标签吗?
  • 我在 HTTP 上下文 (WebApi) 中,没有错误,但 Thread.CurrentPrincipal.Identity.Name 返回原始调用者而不是被篡改的调用者
  • 您是否在 AD 中为应用程序设置了委派?您是否在主机上设置了 SPN?您是否通过 FQDN 连接到服务器?给冥界三头魔犬献了个处女(实习开发者还可以)?
  • 其实……好好想想吧。你知道委托和模仿的区别吗?
  • @Anders 你的WindowsImpersonationContext 为空吗?在您尝试模仿之前和之后,System.Security.Principal.WindowsIdentity.GetCurrent().Name 说了什么?

标签: c# impersonation windows-identity identity-delegation


【解决方案1】:

provider.Authorize(principal, context)System.Security.Principal.IPrincipal 作为其第一个参数(请参阅msdn)。为什么不创建一个System.Security.Principal.WindowsPrincipal-instance(它采用您已经拥有的System.Security.Principal.WindowsIdentity-instance 作为ctor-parameter)并将其用作参数?

否则:你知道线程的CurrentPrincipal 有一个setter 吗?其他请看so-question/answer

【讨论】:

  • 我们在下面的层中执行使用当前主体的代码,但我可以更改线程的主体,因为它不是异步的。将测试并回复您。 (午餐后;))
  • 我刚刚用如何更改线程的CurrentPrincipal 的信息更新了我的答案...
  • 它有效,附带说明,不应调用 Impersonate()。之后 IsInRole 将失败。否则,标准 IsInRole 和 Ent.lib 规则授权都有效!
  • 创建了一篇关于此andersmalmgren.com/2014/06/12/…的小博客文章
  • @Anders 问题在于安全性是一个跨领域的问题。这意味着它的完成方式几乎会受到您对架构做出的每一个决定的影响。
猜你喜欢
  • 1970-01-01
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 2021-06-25
  • 1970-01-01
相关资源
最近更新 更多