在本工作中,您會確定只有一組使用者認證可在您於練習 1:建立基本工作流程服務中建立的工作流程服務上叫用作業。這些認證會以 ClaimSet 的形式傳遞至服務。如需 ClaimSets 的詳細資訊,請參閱Managing Claims and Authorization with the Identity Model。
注意: |
|---|
| 使用 Visual Studio 工作流程設計工具建立或管理工作流程服務時,有時候會產生假性驗證錯誤。如果您能夠成功建置專案,請忽略驗證錯誤。 |
透過 ClaimSets 啟用使用者驗證
如果您尚未開啟 WorkflowServiceTutorial 方案,請開啟 Visual Studio 2008,按一下 [檔案],將 [開啟] 反白顯示,然後巡覽至 WorkflowServiceTutorial 方案。
在 [方案總管] 窗格中的 WorkflowServiceTutorial 專案節點下,以滑鼠右鍵按一下 [參考] 子資料夾,並選取 [加入參考]。或者,如果您建立的是 Visual Basic 方案,請以滑鼠右鍵按一下 WorkflowServiceTutorial 專案節點,並選取 [加入參考]。
在 [加入參考] 對話方塊中的 [NET] 索引標籤下,選取 [System.IdentityModel],然後按一下 [確定]。
在本工作中會使用 ClaimSet 和 List 物件,因此請在 Workflow1.cs 頂端新增下列 using 陳述式:
using System.IdentityModel.Claims; using System.Collections.Generic;如果您建立的是 Visual Basic 方案,請以滑鼠右鍵按一下 WorkflowServiceTutorial 專案節點,並選取 [屬性]。選取 [參考] 索引標籤,並按一下 [匯入的命名空間] 底下 System.IdentityModel.Claims 的核取方塊。已支援 System.Collections.Generic 命名空間。
如果看不到工作流程服務的工作流程設計工具,請以滑鼠右鍵按一下 Workflow1.cs (如果您建立的是 Visual Basic 方案,則為 Workflow1.vb) 並選取 [設計工具檢視],開啟設計工具。
在 Workflow1InitialState StateActivity 活動中,按兩下 WaitToStartService EventDrivenActivity 活動以展開該複合活動。
反白顯示與 StartupService 作業關聯的 ReceiveActivity 活動。
在 [屬性] 窗格的 [OperationValidation] 下,輸入 ValidateUser 並按下 Enter,以自動產生 OperationValidation 事件的事件處理常式。
巡覽至 ValidateUser 事件處理常式。
在 ValidateUser 的主體中,會確認使用者先前已由服務辨識,若未辨識,則不會讓使用者叫用任何作業。例如,如果銷售部門的員工開啟一張採購單,但過了幾天後才回頭完成這張採購單時,這個功能就很有用。您應先驗證這是同一位使用者後,才能讓使用者叫用服務上任何其他的作業。對話 ID 和內容 ID 無法使用,因為它們可能是惡意使用者冒充的。
Private Sub ValidateUser(ByVal sender As System.Object, ByVal e As System.Workflow.Activities.OperationValidationEventArgs) For Each claims As ClaimSet In e.ClaimSets ' Find the claim that contains the name of the operation caller. Dim opCaller As List(Of Claim) = claims.FindClaims(ClaimTypes.Name, Rights.PossessProperty).ToList() ' Retrieve the name of the caller from the claim. Dim opCallerName As String = opCaller(0).Resource.ToString() ' If this is the caller's first time through the ValidationUser method, set ' the operation caller's name to a global variable named "owner." Every subsequent ' operation that uses this method will verify that the caller of ' the operation is the same as the caller of the initial operation before ' either validating or invalidating the caller. If [String].IsNullOrEmpty(owner) Then owner = opCallerName ElseIf Not owner.Equals(opCallerName) Then e.IsValid = False End If Next End Subprivate void ValidateUser(object sender, OperationValidationEventArgs e) { foreach (ClaimSet claims in e.ClaimSets) { // Find the claim that contains the name of the operation caller. List<Claim> opCaller = claims.FindClaims(ClaimTypes.Name, Rights.PossessProperty).ToList<Claim>(); // Retrieve the name of the caller from the claim. string opCallerName = opCaller[0].Resource.ToString(); // If this is the caller's first time through the ValidationUser method, set // the operation caller's name to a global variable named "owner." Every subsequent // operation that uses this method will verify that the caller of // the operation is the same as the caller of the initial operation before // either validating or invalidating the caller. if(String.IsNullOrEmpty(owner)) { owner = opCallerName; } else if (!owner.Equals(opCallerName)) { e.IsValid = false; } } }宣告名為 "owner" 的變數,以便在接收後續作業叫用時使用該變數做為驗證,如下列程式碼所示:
Public class ServerWorkflow Inherits StateMachineWorkflowActivity ' These variables are bound to the input and output parameters of the ReceiveActivity. Public returnValue As Int32 = Nothing Public inputValue As Int32 = Nothing 'This variable contains the user name for the NT account used in operation validation. Public owner As String = Nothing ... End Classpublic sealed partial class ServerWorkflow : StateMachineWorkflowActivity { public ServerWorkflow() { InitializeComponent(); } // These variables are bound to the input and output parameters of the ReceiveActivity. public int returnValue = default(int); public int inputValue = default(int); // This variable contains the user name for the NT account used // in operation validation. public string owner = default(string); ... }對於每個剩下的作業,請將 OperationValidation 事件與 ValidateUser 方法產生關聯。
建置您的方案,並確認您的授權檢查可運作。
如果一個有不同使用者名稱的使用者嘗試在 owner 變數設定後叫用服務上的作業,下列錯誤訊息將會傳回至用戶端:
Security check failed.
注意:ClaimSet 會先處理,然後再處理 PrincipalPermissionRole 或 PrincipalPermissionName,所以如果您對 NT 帳戶群組執行兩個不同的授權檢查,一個使用 ClaimSet,另一個使用 PrincipalPermissionRole,則 ClaimSet 授權檢查會先發生。
請參閱
工作
其他資源
Copyright © 2007 by Microsoft Corporation.All rights reserved.