WF提供了一种用于对所有支持数据输入的活动的、基于角色的访问机制。工作流创建者可以完全控制如何创建角色和角色集合。这样将使创建者能够提供必要的授权机制,在执行活动之前验证调用者的角色。比如WF中的WebServiceInputActivity 和 HandleExternalEventActivity活动。 WF中提供来两种方式: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, 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<BookEventArgs> 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<BookEventArgs> SearchBook; public void OnSearchRequest(Guid instanceId, 4.工作流设计如下: |
通过设置检索事件(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 : 5.通过如下函数来创建角色和用户,代码如下: static void CreateRoles() { if (!System.Web.Security.Roles.RoleExists("会员")) { System.Web.Security.Roles.CreateRole("会员"); string[] users = { "张三", "李四", "王五" }; string[] ClerkRole = { "会员" }; System.Web.Security.Roles. 6.假设以张三的身份来检索,触发事件的函数如下: static void SendSearchRequest() { try { string id = "001"; string name = "C#高级编程"; string author = "某某某"; GenericIdentity genIdentity = 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 = 8.我们要配置aspnetdb数据库,app.config如下: <?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="SqlServerConnection" connectionString="Integrated Security =
9.执行结果如下:(本文由控件中国网转载) |