關於應用技術及操作:
程式語言:C#
前端端:Windows Presentation Foundation (WPF) - .NET Framework 4.6.1
後端:Windows Communication Foundation (WCF) - .NET Framework 4.6.1
恢復所選項目上必要的套件,在PM 控制台中執行以下命令
Update-Package -reinstall
在此應用程式中,我將展示如何在企業架構標準中使用 WCF 服務來使用授權和身份驗證。
世界碳纖維
ASP.NET 驗證服務
自訂認證
HTTP Cookie
授權主體權限屬性
線程 CurrentPrincipal
訊息攔截器
建立 WCF 服務應用程式。
新增重複使用 ASP.NET 驗證服務的 AuthenticationService.svc。
建立使用者驗證器類別。
在 Global.asax 中啟用自訂身份驗證。
如果使用者有效則傳回 Cookie。
修改服務配置。
使用名為 SumOperation、ReadOperation 和 WriteOperation 的三個不同方法建立 ExecuteOperationsService.svc。
使用PrincipalPermission 屬性裝飾服務方法,僅用於授權存取。
使用 AspNetCompatibilityRequirements 屬性裝飾 ExecuteOperationsService 類別。
在伺服器應用程式中實作攔截器。
在服務應用程式中對攔截器實施身分設定代碼。
修改服務端程式碼以包含角色而不是名稱。
使用加密票證來儲存使用者名稱和角色。
建立客戶端應用程式並添加對這兩個服務的引用。
在客戶端應用程式中實作攔截器。
建立身份驗證服務實例並呼叫 Login() 方法。
接收cookie並儲存它。
建立一個 ExecuteOperationsService 實例並呼叫 SumOperation、ReadOperation 和 WriteOperation。
將 Cookie 附加到 ExecuteOperationsService 用戶端
1. 新增重複使用 ASP.NET 驗證服務的 AuthenticationService.svc。
新增新的 WCF 服務並將其命名為 AuthenticationService.svc。刪除關聯的文件,因為我們將公開 ASP.NET 驗證服務。
刪除AuthenticationService.cs
刪除IAuthenticationService.cs
<%@ ServiceHost Language="C#" Service="System.Web.ApplicationServices.AuthenticationService" Factory="System.Web.ApplicationServices.ApplicationServicesHostFactory" %>
2. 在 Global.asax 中啟用自訂身份驗證,如果使用者有效,則傳回 Cookie。
將新專案 Web > Global Application Class新增至專案。
protected void Application_Start(object sender, EventArgs e) { AuthenticationService.Authenticating += AuthenticationService_Authenticating; }
私人無效AuthenticationService_Authenticating(物件發送者,AuthenticatingEventArgs e){字串角色= string.Empty; e.Authenticated = UserIsValid(e, ref 角色); e.AuthenticationIsComplete = true; 如果(e.Authenticated){ 字串加密值 = FormsAuthentication.Encrypt(CreateFormsAuthenticationTicket(e,角色)); OperationContext.Current.OutgoingMessageProperties[HttpResponseMessageProperty.Name] = SetSetCookieInResponseChannelHeaders(encryptedValue); } }
此方法從身份驗證事件參數的自訂憑證物件中提取使用者名稱和密碼。然後它使用我們的 UserValidator 類別驗證使用者名稱和密碼。
如果使用者有效,則屬性 Authenticated 分別表示 true / false。
3. 啟用身份驗證服務和 ASP.NET 相容性
現在我們需要修改 web.config 檔案以包含以下內容:
啟用身份驗證服務
啟用 ASP.NET 相容性
<系統.web.擴充> <腳本> <網路服務> <authenticationService啟用=“true”/> </網路服務> </腳本> </system.web.extensions>
4. 在伺服器應用程式中實作攔截器。
這是因為每次我們需要進行服務呼叫時,將 cookie 附加到操作上下文的上述程式碼似乎是一項乏味的工作。我們可以使用 WCF 攔截器將這些任務移至背景。
MSDN:WCF 資料服務可讓應用程式攔截請求訊息,以便您可以為操作新增自訂邏輯。您可以使用此自訂邏輯來驗證傳入訊息中的資料。您也可以使用它來進一步限制查詢請求的範圍,例如針對每個請求插入自訂授權原則。
以下是此步驟涉及的活動。
在伺服器 web.config 中新增攔截器行為。
建立攔截器行為類別。 (見項目)
建立IdentityMessageInspector類別。 (見項目)
<系統.服務模型> <服務> <服務名稱=“AuthenticationAndAuthorization.ExecuteOperationsService”behaviorConfiguration=“InterceptorBehavior”/> </服務> <行為> <服務行為> <行為> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults =“true”/> </行為> <行為名稱=“InterceptorBehavior”> <serviceDebug includeExceptionDetailInFaults =“true”/> <攔截器行為擴展/> </行為> </服務行為> </行為> <副檔名> <行為擴展> <新增名稱=“interceptorBehaviorExtension”類型=“AuthenticationAndAuthorization.Extensions.IDispatchMessageInspector.InterceptorBehaviorExtension,AuthenticationAndAuthorization,版本= 1.0.0.0,文化=中性”/> </行為擴展> </擴展名> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel>
5. Cookie 加密。
我們的 cookie 包含可由 HTTP 檢查實用程式(如 Fiddler)輕鬆讀取的資訊。這使得cookie資訊容易受到安全威脅。
FormsAuthenticationTicket :System.Web.Security 命名空間為我們提供了一個方便的類別。我們可以將使用者名稱和角色資訊儲存在此類實例中。票證類別還提供過期和加密設施。
私人FormsAuthenticationTicket CreateFormsAuthenticationTicket(AuthenticatingEventArgs e,字串角色)=>新FormsAuthenticationTicket(1,e.UserName,DateTime.Now,DateTime.Now.AddHours(24),true,角色,FormsAuthPathcation.FormsCuthentiookies);
字串加密值 = FormsAuthentication.Encrypt(CreateFormsAuthenticationTicket(e, 角色));私人 FormsAuthenticationTicket GetDecryptTicket(string cryptoTicket) => FormsAuthentication.Decrypt(encryptedTicket);
私有 HttpResponseMessageProperty SetSetCookieInResponseChannelHeaders(string cookieValue) { HttpResponseMessageProperty 回應 = new HttpResponseMessageProperty(); response.Headers[HttpResponseHeader.SetCookie] = FormsAuthentication.FormsCookieName + "=" + cookieValue; 返迴響應; }
<身份驗證模式=“表單”> <forms movingExpiration="true" name="AuthCookie"protection="All" timeout="20"/> </認證> <machineKey解密=「AES」驗證=「SHA1」解密Key=「1523F567EE75F7FB5AC0AC4D79E1D9F25430E3E2F1BCDD3370BCFC4EFC97A541」validation=”33CBA563F F447634ED E8624508A129"/>
6. 在客戶端應用程式中實作攔截器。
以下是此步驟涉及的活動。
在 Client App.config 中新增攔截器行為
建立攔截器行為類別。 (見項目)
建立CookieMessageInspector類別。 (見項目)
<行為> <端點行為> <行為名稱=“InterceptorBehavior”> <攔截器行為擴展/> </行為> </端點行為> </行為>
<端點位址=「http://localhost:8046/ExecuteOperationsService.svc」綁定=「basicHttpBinding」綁定配置=「BasicHttpBinding_IExecuteOperationsService」契約=「ExecuteOperationsServiceReference.IExecute orBehavior”/>