详解在Workflow工作流中如何使用角色

开发 后端
WF提供了一种用于对所有支持数据输入的活动的、基于角色的访问机制。工作流创建者可以完全控制如何创建角色和角色集合。这样将使创建者能够提供必要的授权机制,在执行活动之前验证调用者的角色。

WF(Workflow)中提供来两种方式:ActiveDirectoryRole(通过活动目录用户)和WebWorkflowRole(ASP.NET Role)。下面举例说明:

1.我们使用HandleExternalEventActivity活动来提供图书检索功能,当有人检索的时候会触发检索事件,只有会员才可以使用该功能。首先来定义事件参数:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Workflow.Activities; namespace CaryWFRole { [Serializable] public class BookEventArgs : ExternalDataEventArgs { public string ID { get; set; } public string Name { get; set; } public string Author { get; set; } public BookEventArgs() : base(Guid.NewGuid()) { } public BookEventArgs(Guid instanceID, string id, string name, string author) : base(instanceID) { this.ID = id; this.Name = name; this.Author = author; } } }

2.事件接口如下:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Workflow.Activities; namespace CaryWFRole { [ExternalDataExchangeAttribute()] public interface ISearchBookService { event EventHandler SearchBook; } }

3.实现该接口,代码如下:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Principal; namespace CaryWFRole { public class SearchBookService:ISearchBookService { public event EventHandler SearchBook; public void OnSearchRequest(Guid instanceId, string id,string name,string author, IIdentity identity) { BookEventArgs args = new BookEventArgs(instanceId, id, name, author); String securityIdentifier = null; WindowsIdentity windowsIdentity = identity as WindowsIdentity; if (windowsIdentity != null && windowsIdentity.User != null) securityIdentifier = windowsIdentity.User.Translate(typeof(NTAccount)).ToString(); else if (identity != null) securityIdentifier = identity.Name; args.Identity = securityIdentifier; Console.WriteLine("return book by: {0}", identity.Name); if (SearchBook != null) SearchBook(null, args); } } }

4.工作流设计如下:

image

通过设置检索事件(HandleExternalEventActivity)活动的的Roles属性来控制,只有该角色集合的用户才有权限。在工作流中我们只允许会员才可以做
检索,代码如下:

using System; using System.ComponentModel; using System.ComponentModel.Design; using System.Collections; using System.Drawing; using System.Linq; using System.Workflow.ComponentModel.Compiler; using System.Workflow.ComponentModel.Serialization; using System.Workflow.ComponentModel; using System.Workflow.ComponentModel.Design; using System.Workflow.Runtime; using System.Workflow.Activities; using System.Workflow.Activities.Rules; namespace CaryWFRole { public sealed partial class BookWorkflow : SequentialWorkflowActivity { public BookWorkflow() { InitializeComponent(); } private WorkflowRoleCollection sAllowRoles = new WorkflowRoleCollection(); public WorkflowRoleCollection AllowRoles { get { return sAllowRoles; } } private void codeActivity1_ExecuteCode(object sender, EventArgs e) { WebWorkflowRole role = new WebWorkflowRole("会员"); AllowRoles.Add(role); } private void handleExternalEventActivity1_Invoked(object sender, ExternalDataEventArgs e) { Console.WriteLine("查询成功"); } } }

5.通过如下函数来创建角色和用户,代码如下:


static void CreateRoles() { if (!System.Web.Security.Roles.RoleExists("会员")) { System.Web.Security.Roles.CreateRole("会员"); string[] users = { "张三", "李四", "王五" }; string[] ClerkRole = { "会员" }; System.Web.Security.Roles.AddUsersToRoles(users, ClerkRole); } }

6.假设以张三的身份来检索,触发事件的函数如下:


static void SendSearchRequest() { try { string id = "001"; string name = "C#高级编程"; string author = "某某某"; GenericIdentity genIdentity = new GenericIdentity("张三"); sBook.OnSearchRequest(workflowInstanceId, id, name, author, genIdentity); } catch (Exception e) { Console.WriteLine("Exception message: {0}", e.ToString()); } }

7.宿主程序如下:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Workflow.Runtime; using System.Workflow.Runtime.Hosting; using System.Security.Principal; using System.Workflow.Activities; namespace CaryWFRole { class Program { static SearchBookService sBook; static Guid workflowInstanceId; static AutoResetEvent waitHandle = new AutoResetEvent(false); static void Main() { CreateRoles(); using (WorkflowRuntime workflowRuntime = new WorkflowRuntime()) { workflowRuntime.StartRuntime(); Type type = typeof(BookWorkflow); ExternalDataExchangeService dataService = new ExternalDataExchangeService(); workflowRuntime.AddService(dataService); sBook = new SearchBookService(); dataService.AddService(sBook); workflowRuntime.WorkflowCompleted += OnWorkflowCompleted; workflowRuntime.WorkflowTerminated += OnWorkflowTerminated; WorkflowInstance instance = workflowRuntime.CreateWorkflow(type); workflowInstanceId = instance.InstanceId; instance.Start(); SendSearchRequest(); waitHandle.WaitOne(); workflowRuntime.StopRuntime(); } } static void OnWorkflowCompleted(object sender, WorkflowCompletedEventArgs e) { waitHandle.Set(); } static void OnWorkflowTerminated(object sender, WorkflowTerminatedEventArgs e) { Console.WriteLine(e.Exception.Message); waitHandle.Set(); } } }

8.我们要配置aspnetdb数据库,app.config如下:

<?xml version="1.0" encoding="utf-8" ?>
<CONFIGURATION>
    <CONNECTIONSTRINGS>
        <ADD name="SqlServerConnection" connectionString="Integrated Security = SSPI;server=.;database=aspnetdb" />
    </CONNECTIONSTRINGS>
    <SYSTEM.WEB>
        <ROLEMANAGER defaultProvider="SqlProvider" enabled="true">
            <PROVIDERS>
                <ADD name="SqlProvider" type="System.Web.Security.SqlRoleProvider, &#13;&#10;                System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" applicationName="ConsoleAppSample" connectionStringName="SqlServerConnection" />
            </PROVIDERS>
        </ROLEMANAGER>
    </SYSTEM.WEB>
</CONFIGURATION>

9.执行结果如下:

image


【编辑推荐】

  1. 详解工作流架构与实现
  2. 协同软件:对工作流的几点谬误的解析
  3. 面向构件的中间件:超越工作流管理
责任编辑:彭凡 来源: cnblogs
相关推荐

2021-10-14 11:34:05

技术工作流引擎

2009-11-18 09:14:49

Visual Stud

2011-02-21 13:21:20

.NET Workfl

2009-03-03 09:13:36

工作流BPM业务流程

2010-01-14 09:35:10

WF4.0

2013-09-29 17:13:59

PowerShell工作流

2022-10-26 08:00:43

Activiti工作流BPM

2021-03-12 06:44:09

Argo Workfl开源项目

2009-06-11 14:43:34

jbpm工作流引擎jBPM搭建

2010-11-26 10:59:28

SharePoint

2013-04-23 10:28:08

IBeamMDAAWF

2022-07-14 10:06:20

工作流引擎营销自动化vivo

2022-07-10 21:17:01

GitTigLinux

2009-03-27 09:48:56

SnapFlowWaaS工作流

2018-06-04 17:56:58

开发者故事

2010-05-28 15:16:33

SharePoint 工作流

2011-09-20 09:41:29

.NET 4.5

2011-12-14 09:58:58

JavajBPM

2023-07-05 09:48:44

Activiti部署

2012-07-23 10:36:46

工作流
点赞
收藏

51CTO技术栈公众号