[英]Pass SecurityException through multiple hops of WCF services
我有一個充當WCF客戶端的ASP.NET MVC / WebApi應用程序。 我的WCF服務正在通過ClaimsPrincipalPermission
進行授權,並且如果當前用戶和數據的組合不匹配(例如,用戶正在嘗試從其他用戶訪問數據),則還會拋出SecurityException
。
我在過濾器中將其作為SecurityAccessDeniedException
捕獲,並返回狀態代碼403。
但是,這僅適用於涉及單跳的通信。 如果服務本身與另一個服務進行通信,則SecurityAccessDeniedException
在到達Web應用程序之前將轉換為FaultException
。
流程示例:
Web client --> Service A [SecurityException] --> [SecurityAccessDeniedException] Web
Web client --> Service A --> Service B [SecurityException]
--> [SecurityAccessDeniedException] Service A [FaultException] --> [FaultException] Web
我嘗試實現一個處理SecurityAccessDeniedException
的IErrorHandler
,但是我不能僅僅在此處拋出SecurityException
。
我有什么選擇可以將SecurityException
傳播到Web客戶端?
或者:如何在錯誤處理程序中構造一條消息,然后客戶端將其正確解釋為SecurityAccessDeniedException
?
我現在正在使用IErrorHandler
,它生成相應的FaultException
。 在調用方上,這將生成另一個SecurityAccessDeniedException
,然后可以使用相同的IErrorHandler
等進行處理。
public void ProvideFault(Exception error, MessageVersion version, ref Message message)
{
if (error is SecurityAccessDeniedException)
{
var code = FaultCode.CreateSenderFaultCode(
"FailedAuthentication",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
var faultText = new FaultReasonText(error.Message, CultureInfo.CurrentCulture);
var faultException = new FaultException(new FaultReason(faultText), code);
MessageFault messageFault = faultException.CreateMessageFault();
message = Message.CreateMessage(version, messageFault, "FailedAuthentication"); // the last parameter might not be semantically correct...
}
}
我通過查看http://leastprivilege.com/2007/11/01/wcf-and-securityaccessdeniedexception/獲得了這個想法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.