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

And*_*ers 5 c# impersonation windows-identity identity-delegation

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

我正在考虑模仿线程,然后进行授权.我发现了这篇文章

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();
}
Run Code Online (Sandbox Code Playgroud)

之后ctx = wi.Impersonate();的线程仍然在主叫用户的情况下,我究竟做错了什么?

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

provider = AuthorizationFactory.GetAuthorizationProvider();
provider.Authorize(Thread.CurrentPrincipal, Rule)
Run Code Online (Sandbox Code Playgroud)

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

小智 0

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

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