Csharp/C#教程:调用Task.WaitAll()时如何设置任务的用户身份?分享


调用Task.WaitAll()时如何设置任务的用户身份?

我遇到了并行调用WCF服务的问题。 我正在尝试使用Tasks来并行调用两个服务以节省一些时间(我不是试图使这个异步),但我的任务是作为另一个用户运行的。 这两种服务都需要Windows身份validation,并且两次调用都会失败,因为调用该服务的用户没有所需的权限。

调用代码(下面的代码)位于ASP.NET MVC4中的Controller Action内,运行IIS 7.5。 涉及的所有应用程序池都设置为允许ASP.NET模拟并需要Windows身份validation(所有其他方法设置为禁用)。

// Parallel service request to two services Task[] tasks = new Task[2]; // this shows my credentials, eg "MYDOMAIN\MyUsername" var userMe = System.Security.Principal.WindowsIdentity.GetCurrent().Name; tasks[0] = Task.Factory.StartNew(() => { // this shows "IIS APPPOOL\DefaultAppPool" var user = System.Security.Principal.WindowsIdentity.GetCurrent().Name; var data = client1.MyMethod(); return data; }); tasks[1] = Task.Factory.StartNew(() => { // this shows "IIS APPPOOL\DefaultAppPool" var user = System.Security.Principal.WindowsIdentity.GetCurrent().Name; var data = client2.MyMethod(); return data; }); Task.WaitAll(tasks); 

我的问题:如何指示任务作为当前用户运行,而不是作为“IIS APPPOOL DefaultAppPool”用户运行?

试试这个:

 WindowsIdentity impersonatedUser = WindowsIdentity.GetCurrent(); Task.Factory.StartNew(() => { using (WindowsImpersonationContext ctx = impersonatedUser.Impersonate()) { //Your code } return data; }); 

您可能想要查看设置是否合适:

指定Windows标识始终跨异步点流动,无论模拟的执行方式如何。

默认情况下,此设置在ASP.Net中是关闭的,因此任务(可能在其他线程上运行)最终运行时没有模拟。

否则(如果设置不合适),我认为你可以将WindowsIdentity对象捕获到由Task捕获的局部变量中,然后从任务中调用Impersonate() 。 (虽然我没试过这个)

上述就是C#学习教程:调用Task.WaitAll()时如何设置任务的用户身份?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/992202.html

(0)
上一篇 2021年12月24日
下一篇 2021年12月24日

精彩推荐