gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:51:05 24 4
gpt4 key购买 nike

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

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

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,您已经拥有它作为 ctor 参数)并将其用作参数?

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

关于c# - 将线程的上下文更改为其他用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24180234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com