简体   繁体   中英

Windows Workflow and Thread.CurrentPrincipal

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="[&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>

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

I believe that since Workflow s 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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM