将线程的上下文更改为其他用户

本文关键字:其他 用户 线程 上下文 | 更新日期: 2023-09-27 18:08:12

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

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

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/

将线程的上下文更改为其他用户

provider.Authorize(principal, context)的第一个参数是System.Security.Principal.IPrincipal(参见msdn)。为什么不创建一个System.Security.Principal.WindowsPrincipal -instance(它以您已经拥有的System.Security.Principal.WindowsIdentity -instance作为参数)并使用它作为参数呢?

否则:你知道线程的CurrentPrincipal有setter吗?请参阅其他问题/答案