Windows 工作流和 Thread.CurrentPrincipal

发布于 2024-10-30 23:08:51 字数 3591 浏览 0 评论 0原文

我遇到 Thread.CurrentPrincipal 未从主线程传播到工作流应用程序的问题。

有办法做到这一点吗?

这是我的示例应用程序:

class Program
{
    static void Main(string[] args)
    {
        Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("Bob", "Passport"), new string[] { "managers", "executives" });

        System.Console.WriteLine("MainThread Prinicipal type: " + Thread.CurrentPrincipal.GetType().ToString());
        System.Console.WriteLine("MainThread Prinicipal Identity: " + Thread.CurrentPrincipal.Identity.Name);
        System.Console.WriteLine();

        AutoResetEvent syncEvent = new AutoResetEvent(false);

        WorkflowApplication application = new WorkflowApplication(new Workflow1());

        application.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
        {
            syncEvent.Set();
        };

        application.Run();
        syncEvent.WaitOne();
    }
}

和工作流程

<Activity mc:Ignorable="sap" x:Class="WorkflowConsoleApplication1.Workflow1" sap:VirtualizedContainerService.HintSize="273,427" mva:VisualBasic.Settings="Assembly references and imported namespaces for internal implementation" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="clr-namespace:Microsoft.VisualBasic;assembly=System" xmlns:mva="clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:s1="clr-namespace:System;assembly=System" xmlns:s2="clr-namespace:System;assembly=System.Xml" xmlns:s3="clr-namespace:System;assembly=System.Core" xmlns:s4="clr-namespace:System;assembly=System.ServiceModel" xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities" xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=System" xmlns:scg1="clr-namespace:System.Collections.Generic;assembly=System.ServiceModel" xmlns:scg2="clr-namespace:System.Collections.Generic;assembly=System.Core" xmlns:scg3="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:sd="clr-namespace:System.Data;assembly=System.Data" xmlns:sl="clr-namespace:System.Linq;assembly=System.Core" xmlns:st="clr-namespace:System.Threading;assembly=System.Core" xmlns:st1="clr-namespace:System.Threading;assembly=System" xmlns:st2="clr-namespace:System.Text;assembly=mscorlib" xmlns:st3="clr-namespace:System.Threading;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Sequence sad:XamlDebuggerXmlReader.FileName="C:\projects\WorkflowConsoleApplication1\WorkflowConsoleApplication1\Workflow1.xaml" sap:VirtualizedContainerService.HintSize="233,387">
        <WriteLine sap:VirtualizedContainerService.HintSize="211,61" Text="[&quot;WorkflowThread Prinicipal type: &quot; + System.Threading.Thread.CurrentPrincipal.GetType().ToString()]" />
        <WriteLine sap:VirtualizedContainerService.HintSize="211,61" Text="[&quot;WorkflowThread Prinicipal Identity: &quot; + System.Threading.Thread.CurrentPrincipal.Identity.Name]" />
        <WriteLine sap:VirtualizedContainerService.HintSize="211,61" />
    </Sequence>
</Activity>

上述程序的输出是:

MainThread主体类型:System.Security.Principal.GenericPrincipal
主线程主体身份:Bob

WorkflowThread 主要类型:System.Security.Principal.GenericPrincipal
WorkflowThread 主要身份:

任何帮助将不胜感激。

谢谢

I am having an issue with Thread.CurrentPrincipal not being propagated from the main thread to the workflow application.

Is there anyway to do this?

Here is my sample application:

class Program
{
    static void Main(string[] args)
    {
        Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("Bob", "Passport"), new string[] { "managers", "executives" });

        System.Console.WriteLine("MainThread Prinicipal type: " + Thread.CurrentPrincipal.GetType().ToString());
        System.Console.WriteLine("MainThread Prinicipal Identity: " + Thread.CurrentPrincipal.Identity.Name);
        System.Console.WriteLine();

        AutoResetEvent syncEvent = new AutoResetEvent(false);

        WorkflowApplication application = new WorkflowApplication(new Workflow1());

        application.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
        {
            syncEvent.Set();
        };

        application.Run();
        syncEvent.WaitOne();
    }
}

and the workflow

<Activity mc:Ignorable="sap" x:Class="WorkflowConsoleApplication1.Workflow1" sap:VirtualizedContainerService.HintSize="273,427" mva:VisualBasic.Settings="Assembly references and imported namespaces for internal implementation" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="clr-namespace:Microsoft.VisualBasic;assembly=System" xmlns:mva="clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:s1="clr-namespace:System;assembly=System" xmlns:s2="clr-namespace:System;assembly=System.Xml" xmlns:s3="clr-namespace:System;assembly=System.Core" xmlns:s4="clr-namespace:System;assembly=System.ServiceModel" xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities" xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=System" xmlns:scg1="clr-namespace:System.Collections.Generic;assembly=System.ServiceModel" xmlns:scg2="clr-namespace:System.Collections.Generic;assembly=System.Core" xmlns:scg3="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:sd="clr-namespace:System.Data;assembly=System.Data" xmlns:sl="clr-namespace:System.Linq;assembly=System.Core" xmlns:st="clr-namespace:System.Threading;assembly=System.Core" xmlns:st1="clr-namespace:System.Threading;assembly=System" xmlns:st2="clr-namespace:System.Text;assembly=mscorlib" xmlns:st3="clr-namespace:System.Threading;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Sequence sad:XamlDebuggerXmlReader.FileName="C:\projects\WorkflowConsoleApplication1\WorkflowConsoleApplication1\Workflow1.xaml" sap:VirtualizedContainerService.HintSize="233,387">
        <WriteLine sap:VirtualizedContainerService.HintSize="211,61" Text="["WorkflowThread Prinicipal type: " + System.Threading.Thread.CurrentPrincipal.GetType().ToString()]" />
        <WriteLine sap:VirtualizedContainerService.HintSize="211,61" Text="["WorkflowThread Prinicipal Identity: " + System.Threading.Thread.CurrentPrincipal.Identity.Name]" />
        <WriteLine sap:VirtualizedContainerService.HintSize="211,61" />
    </Sequence>
</Activity>

The output from the above program is:

MainThread Prinicipal type: System.Security.Principal.GenericPrincipal
MainThread Prinicipal Identity: Bob

WorkflowThread Prinicipal type: System.Security.Principal.GenericPrincipal
WorkflowThread Prinicipal Identity:

Any help will be greatly appreciated.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过期以后 2024-11-06 23:08:51

WorkflowApplication 使用其 SynchronizationContext 来安排实际工作。默认实现使用ThreadPool如user7116所述。不过,您可以自由地实现自己的 SynchronizationContext 并实现 Post() 操作来执行您想要的任何操作。

The WorkflowApplication uses its SynchronizationContext to schedule actual work. The default implementation uses the ThreadPool as user7116 described. You are however free to implement your own SynchronizationContext and implement the Post() operation to do whatever you want.

二智少女猫性小仙女 2024-11-06 23:08:51

我相信,由于工作流在线程池上运行,它们不会继承您在主线程上设置的任何主体。您可以尝试更改AppDomain的主体

// per MSDN: Create a principal to use for new threads.
IPrincipal principal = new GenericPrincipal(
    new GenericIdentity("Bob", "Passport"),
    new string[] { "managers", "executives" });
AppDomain.CurrentDomain.SetThreadPrincipal(principal);

I believe that since Workflows operate on the ThreadPool they will not inherit any principal you set on the main thread. You could try changing the principal of the AppDomain:

// per MSDN: Create a principal to use for new threads.
IPrincipal principal = new GenericPrincipal(
    new GenericIdentity("Bob", "Passport"),
    new string[] { "managers", "executives" });
AppDomain.CurrentDomain.SetThreadPrincipal(principal);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文