這篇文章主要介紹在Workflow工作流中怎么使用角色,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
站在用戶的角度思考問題,與客戶深入溝通,找到衡陽縣網站設計與衡陽縣網站推廣的解決方案,憑借多年的經驗,讓設計與互聯網技術結合,創造個性化、用戶體驗好的作品,建站類型包括:網站制作、成都網站制作、企業官網、英文網站、手機端網站、網站推廣、域名與空間、網站空間、企業郵箱。業務覆蓋衡陽縣地區。
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 EventHandlerSearchBook;
}
}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 EventHandlerSearchBook;
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());
}
}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, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" applicationName="ConsoleAppSample" connectionStringName="SqlServerConnection" /> </PROVIDERS> </ROLEMANAGER> </SYSTEM.WEB> </CONFIGURATION>
以上是“在Workflow工作流中怎么使用角色”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注創新互聯行業資訊頻道!
本文標題:在Workflow工作流中怎么使用角色
網頁URL:http://www.yijiale78.com/article12/pehhgc.html
成都網站建設公司_創新互聯,為您提供手機網站建設、企業建站、App設計、網站制作、虛擬主機、品牌網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯