gpt4 book ai didi

sql - WCF 服务中的 TPL 任务无法使用正确的 IIS 安全凭据(SQL 连接)

转载 作者:行者123 更新时间:2023-12-04 14:12:07 24 4
gpt4 key购买 nike

我有一个调用 SQL 存储过程的 WCF 服务方法。我正在使用 IIS 5 进行开发(对此无能为力,II6/7 不可用)

为了获得一些 yield ,我通过将调用放入 c# TPL 任务中对该存储过程进行了多次异步调用。

当作为任务运行时,我得到一个 SQL 异常......
“登录失败。登录来自不受信任的域,不能用于 Windows 身份验证”

但是,如果我在不使用任务的情况下运行完全相同的进程,则 SQL 连接没有问题

在我看来,IIS 虚拟文件夹 (WCF) 的凭据没有被委派给任务?有什么想法可以为 TPL 任务线程指定凭据,即使用与父级相同的凭据等吗?

我正在使用 Windows 身份验证 (sspi) 和模拟,以便能够连接到单独的 SQL 框。

感谢您的帮助。

最佳答案

你有两个选择。

1)选择您的整个应用程序始终使用以下方式流动身份:

<runtime>
<alwaysFlowImpersonationPolicy enabled="true"/>
</runtime>

这会产生开销的副作用,并且存在使用当前调用用户的权限而不是应用程序身份意外执行某些意外代码的危险。我个人会避免这种情况,并使用您明确选择加入的#2。

2) 捕获 WindowsIdentity 在设置您的 TPL 任务并使用 Impersonate 明确模拟您需要调用电话的位置之前+ WindowsImpersonationContext :
public void SomeWCFOperation()
{
WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();

Task.Factory.StartNew(() =>
{
// some unpriviledged code here


using(WindowsImpersonationContext impersonationContext = currentIdentity.Impersonate())
{
// this code will execute with the priviledges of the caller
}

// some more unpriviledged code here
});
}

关于sql - WCF 服务中的 TPL 任务无法使用正确的 IIS 安全凭据(SQL 连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10007535/

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