在WF中使用角色
2010-10-01 08:24:44 来源:WEB开发网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 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.执行结果如下:
更多精彩
赞助商链接