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, string name, string author) : base(instanceID) { this.ID = id; this.Name = name; this.Author = author; } } }
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, 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.工作流设计如下:
通过设置检索事件(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()); } }
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 enabled="true" defaultProvider="SqlProvider"> <providers> <add name="SqlProvider" connectionStringName="SqlServerConnection" applicationName="ConsoleAppSample" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> </providers> </roleManager> </system.web> </configuration>
9.执行结果如下:
发表评论
-
平淡的2007
2007-12-24 08:04 785早上起来,送女朋友去公交车站,然后回来赶紧打开电脑,先 ... -
DreamSpark发布,高校学生免费使用Visual Studio 2008 Professional Edition 等微软软件
2008-02-20 13:23 1383今天上网无意中搜索到学生可以免费使用VS2008专业版,后来又 ... -
坚持学习WF(1):从HelloWorld开始
2008-04-04 16:30 850[置顶]坚持学习WF文章索 ... -
坚持学习WF(2):WF创作模式和设计时工具
2008-04-05 17:19 600[置顶]坚持学习WF文章索 ... -
坚持学习WF(3):WF框架概览
2008-04-08 07:27 738[置顶]坚持学习WF文章索 ... -
坚持学习WF(4):活动(Activity)和依赖属性(DependencyProperty)
2008-04-12 00:01 1103[置顶]坚持学习WF文章索引 活动(Activity) 活动 ... -
坚持学习WF(5):自定义活动(CustomActivity)
2008-04-13 15:25 882当WF提供的标准活动不能满足我们的需求的时候,我们就需要定义自 ... -
MOSS点滴(1):如何开发和部署feature
2008-04-16 21:35 807Features 是MOSS 2007以开箱即用的一套新功能, ... -
MOSS点滴(2):自定义Application Page
2008-04-19 20:07 810在MOSS中后台管理的页面都是Application Pag ... -
坚持学习WF(6):开发可复用的宿主程序
2008-04-21 21:45 662我们之前写工作流宿主 ... -
MOSS点滴(3):说说MOSS中的母版页
2008-04-25 21:15 1139MOSS中有两种页面:Site P ... -
MOSS点滴(4):实现Form认证
2008-04-29 21:12 669本文主要参考了网上的一些文章,但有些文章有些地方说的不是很明确 ... -
坚持学习WF(7):流程控制(Flow Control)
2008-04-30 18:10 775本文主要说说WF中和流 ... -
坚持学习WF(8):本地服务之调用外部方法
2008-05-09 08:17 719WF提供了一组核心服务 ... -
MOSS中的WebPart开发
2008-05-10 13:53 1025由于在asp.net1.1的时候asp.net中还没有webp ... -
坚持学习WF(9):本地服务之事件处理
2008-05-28 07:49 764[置顶]坚持学习WF文章索引 一:先来介绍两个活动 Even ... -
坚持学习WF(10):在工作流中使用关联
2008-06-01 13:03 660[置顶]坚持学习WF文章索 ... -
坚持学习WF(11):工作流通信与队列
2008-06-07 15:45 696[置顶]坚持学习WF文章索引 WF 提供的通信模型是构建于 ... -
MOSS中创建自定义内容类型
2008-06-12 20:23 1072一:简要介绍 某类内容 ... -
.NET中IDisposable接口的基本使用
2008-06-15 12:01 910首先来看MSDN中关于这个接口的说明: [ComVisible ...
相关推荐
wf工作流学习入门的经典资料,详细介绍了工作流中的授权处理逻辑
OtherNamespace 在XAML中使用其他项目中的命名空间。 PropertyandTypeConvert 使用属性和类型转换器 PropertyElementSyntax 使用XAML的属性元素语法。 UnderstandXAMLDemo1 理解XAML的简单示例。 ...
C#语法知识、C#的面向对象特性、ASP.NET数据绑定技术、高级数据绑定控件、缓存和异步访问、LINQ语言集成查询、成员管理技术、自定义成员提供者、角色和授权、在ASP.NET中整合WCF编程、在ASP.NET中整合WF编程、HTTP...
在实施SOA的过程中,可以自己使用WCF+WF搭建一个小型轻量级的SOA框架,也可以使用诸如Biztalk等软件。 三、注意事项: 1、开发环境为Visual Studio 2010及以上,数据库为SQL Server 2008R2,数据库文件在DB文件夹中...
用SQlServer 2005实现单实例多租赁的数据模型,使用ADAM与ASP.NET2.0membership来实现用户和角色的安全管理等等。这些技术的优势与特点都在Crab这个案例里充分的体现出来,对于刚接触asp.net3.0的程序员来说,Crab...
创意、新奇、有趣、实用的开源应用、系统、软件、硬件及技术,一个探索、发现、分享、使用与互动交流的开源技术社区平台。致力于打造活力开源社区,共建开源新生态! 功能简介 个人办公:待办事项、抄送传阅、办理...
WCF 数据服务是 .NET Framework 的一个组件,可以利用此服务创建基于 REST 的服务和应用程序,以便使用开放式数据协议 (OData) 在 Web 上公开和使用数据。 WCF 数据服务具有多项新功能,包括增强的 BLOB 支持、数据...